2017-11-23 21 views
1

文字列パラメータをJavaメソッドに渡して、dbから特定のオブジェクトを取得します。パラメータをSQL文字列に渡すときに問題が発生しました。java.sql.SQLException:無効な列名ERROR

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

テーブル構造コード – sasikumar

+0

を表示してください。また、getProductFromResultSet()コードを表示してください。 –

+0

getProductFromResultSet(rsEmp)をペーストできますか?メソッドのソースコード –

答えて

1

あなたのMySQLクエリ列名IDを確認してください。 Linux OSのテーブル名を使用している場合は、大文字と小文字を区別します。テーブルの名前が大文字と小文字のどちらでも正しいことを確認してください。