我的 jdbc 驱动程序出现此错误。我也不知道为什么。
下面是相应的代码:
try {
String colNames = " ";
for (int i=0; i<cols.size(); i++) {
if (i == cols.size()-1) {
colNames += cols.get(i);
} else if (i<cols.size()) {
colNames += cols.get(i)+", ";
}
}
String colValues = " ";
for (int i=0; i<values.size(); i++) {
if (i == values.size()-1) {
colValues += values.get(i);
} else if (i<values.size()) {
colValues += values.get(i) + ", ";
}
}
System.out.println(
"INSERT INTO `" + tableName + "` (" + colNames + ") VALUES (" + colValues + ") "
);
//System.out.println(kerdojel);
PreparedStatement pst = connHandler.conn.prepareStatement
("INSERT INTO `" + tableName + "` (" + colNames + ") VALUES (" + colValues + ") ");
pst.executeUpdate();
pst.close();
}
“values”和“cols”是包含 JTable 中的数据的 ArrayList。 cols 是列名称,values 是单元格值。
系统输出的输出:
INSERT INTO `TableOne` ( nev, kor, lakhely) VALUES ( asd, 1, asd)
错误:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'asd' in 'field list'
最佳答案
这不是 PreaparedStatement
的用途。当您使用 PreparedStatement
时,您可以使用“set”方法之一指定值。
这是一个例子:
String colNames = " ";
String colValues = " ";
for (int i=0; i<cols.size(); i++) {
if(i!=0){
colNames += ", ";
colValues += ", ";
}
colNames += cols.get(i);
colValues += "?";
}
try (PreparedStatement pst = connHandler.conn.prepareStatement("INSERT INTO `" + tableName + "` (" + colNames + ") VALUES (" + colValues + ") ");){
for (int i = 0; i < values.size(); i++) {
pst.setString(i+1,values.get(i));
}
pst.executeUpdate();
}
您应该根据列的数据类型使用适当的“set”方法(setInt(...)
、setDate(...)
等) 。您可以查看更多详情here
关于Java - 'field list' jdbc 错误中的未知列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36238867/