Ragnar921
Ragnar921

Reputation: 967

java.sql.SQLException: Invalid column name ERROR

I am passing a string parameter to a java method to get a particular object from the db. Faced some trouble when passing the parameter to the SQL string.

    //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;
    }
}

It occurs the above error. The column is there and the dbQuery also working fine. The query also looks fine.

SELECT * FROM product WHERE id = 'G002'

Where could I go possibly wrong?

The table structure:

+-------------+--------------+------+-----+---------+-------+
| 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    |       |
+-------------+--------------+------+-----+---------+-------+

Upvotes: 1

Views: 3694

Answers (1)

Nisal Edu
Nisal Edu

Reputation: 7591

Check your MySQL query column name id. If you are using Linux OS table name act as case sensitive manner. So make sure your table name is correct with upper cases and lower cases

Upvotes: 1

Related Questions