我将字符串参数传递给 java 方法以从数据库中获取特定对象。将参数传递给 SQL 字符串时遇到一些麻烦。
//SELECT a Product
public static Product searchProduct (String productId) throws SQLException, ClassNotFoundException {
//Declare a SELECT statement
String selectStmt = "SELECT * FROM product WHERE id = '"+ productId +"'";
//Execute SELECT statement
try {
//Get ResultSet from dbExecuteQuery method
ResultSet rsEmp = DBUtil.dbExecuteQuery(selectStmt);
//Send ResultSet to the getProductFromResultSet method and get product object
Product product = getProductFromResultSet(rsEmp);
//Return product object
return product;
} catch (SQLException e) {
System.out.println("While searching an product with '" + productId + "' id, an error occurred: " + e);
//Return exception
throw e;
}
}
出现上述错误。该列在那里,dbQuery 也工作正常。查询看起来也不错。
SELECT * FROM product WHERE id = 'G002'
我可能哪里出错了?
表结构:
+-------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+-------+
| id | varchar(20) | NO | PRI | NULL | |
| title | varchar(200) | YES | | NULL | |
| type | varchar(200) | YES | | NULL | |
| description | varchar(200) | YES | | NULL | |
| unit_price | varchar(20) | YES | | NULL | |
| quantity | varchar(20) | YES | | NULL | |
+-------------+--------------+------+-----+---------+-------+
最佳答案
检查您的 MySQL 查询列名称 ID。如果您使用的是 Linux OS 表名,则以区分大小写的方式进行处理。所以请确保您的表名大小写正确
关于java.sql.SQLException : Invalid column name ERROR 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47449162/