2012-11-09 174 views
0

我正在用下面的代碼查詢Microsoft Access數據庫。數據庫字段名稱在SELECT語句中正確聲明。試圖找出爲什麼我得到這個錯誤。真的需要一些help..thanksjava.sql.SQLException:未找到列錯誤?

public Item getIteminfo(String itemCode) throws ClassNotFoundException, SQLException { 
    Statement myStatement = getConnection(); 
    Item item = null; 
    String itemDescription; 
    int itemPrice; 
    String sql = "SELECT ItemDescription, ItemPrice FROM itemCatalog WHERE ItemCode = '"+itemCode+"'"; 
    ResultSet results = myStatement.executeQuery(sql); 

    while (results.next()){ 
     itemDescription = results.getString("ItemDescription"); 
     itemPrice = results.getInt("ItemPrice"); 
     item = new Item(itemDescription, itemPrice); 
    } 
    closeConnection(); 
    return item; 
} 

這裏的錯誤消息:

java.sql.SQLException: Column not found 
    at sun.jdbc.odbc.JdbcOdbcResultSet.findColumn(JdbcOdbcResultSet.java:1849) 
    at sun.jdbc.odbc.JdbcOdbcResultSet.getString(JdbcOdbcResultSet.java:410) 
    at checkoutsimulation.DAO.getIteminfo(DAO.java:52) 
    at checkoutsimulation.ItemCatalog.getItemdetails(ItemCatalog.java:61) 
    at checkoutsimulation.CheckoutSystem.bnPurchaseActionPerformed(CheckoutSystem.java:463) 
    at checkoutsimulation.CheckoutSystem.access$100(CheckoutSystem.java:20) 

編輯:的字段是相同的,這裏有一個屏幕截圖 enter image description here

+2

你能檢查數據庫中列名的大小寫與你指定的大小寫是否完全一致嗎? – Igor

+0

yup ..這些字段是相同的,請參閱屏幕截圖 – Adesh

+0

您是否調試過並看到天氣有任何結果?或者結果集是空的? – andunslg

回答

2

我用不上自己所以我不能嘗試這個,但是這裏可能有效。首先,我們打印出結果集中列的名稱,以便在工作中有一些區分大小寫的情況。

然後,我們使用位置參數(1,2,..)而不是名稱來執行int result.next循環的解決方法。無論名字是什麼,這應該使它工作。

一旦找出名稱問題,用正確的名稱替換1,2等。

ResultSet results = myStatement.executeQuery(sql); 

ResultSetMetaData meta = results.getMetaData(); 
for (int index = 1; index <= meta.getColumnCount(); index++) 
{ 
    System.out.println("Column " + index + " is named " + meta.getColumnName(index); 
} 
while (results.next()){ 
    itemDescription = results.getString(1); 
    itemPrice = results.getInt(2); 
    item = new Item(itemDescription, itemPrice); 
} 
+0

嘿謝謝..現在這個,給我一下。 – Adesh

+0

仍然收到相同的錯誤..感謝雖然 – Adesh

+0

是否有任何「列1命名...」行打印出來?我想知道你是否得到沒有列的結果集。也許增加一個println來顯示列數。 –

相關問題