java.sql.SQLException:无效的列名ERROR

时间:2017-11-23 06:44:22

标签: java mysql

我将字符串参数传递给java方法以从db获取特定对象。将参数传递给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    |       |
+-------------+--------------+------+-----+---------+-------+

1 个答案:

答案 0 :(得分:1)

检查您的MySQL查询列名称ID。如果您使用Linux OS表名称作为区分大小写的方式。因此,请确保您的表名在大写和小写的情况下是正确的