1

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    |       |
+-------------+--------------+------+-----+---------+-------+
5
  • show your table structure code Commented Nov 23, 2017 at 6:46
  • Also, please show getProductFromResultSet() code Commented Nov 23, 2017 at 6:47
  • can you paste getProductFromResultSet(rsEmp); method source code Commented Nov 23, 2017 at 6:53
  • You should learn to use prepared statements instead of concatenating strings into SQL. Commented Nov 23, 2017 at 7:17
  • Also, stack trace is missing... Commented Nov 23, 2017 at 7:26

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.