java.sql.SQLException : Invalid column name ERROR 异常

标签 java mysql

我将字符串参数传递给 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/

相关文章:

java - 在 Java 中使用正则表达式从字符串中提取值

java - 如何在java中进行持续的UI开发?

java - 将记录从数组插入到 Sqlite 数据库 (Android)

java - 在 Websphere Application Server 7 上运行简单的 JAX-RS 应用程序时出现 null 错误

java - 如何计算返回的查询MySQL临时表的中位数

c# - 为什么我的 INSERT 语句会产生重复?

java - "ValueError: Incompatible Language version 13. Must not be between 9 and 12"与 Google Colab

php - MySQL PHP 跨数据库事务

mysql - 是否有可能获得 MySQL 中某个组的子计数

mysql - 如何在 1 个查询中使用 COUNT() 和 SUM()