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 | |
+-------------+--------------+------+-----+---------+-------+
getProductFromResultSet()code