簡體   English   中英

java.sql.SQLException:找不到列錯誤?

[英]java.sql.SQLException: Column not found Error?

我使用下面的代碼查詢Microsoft Access數據庫。 SELECT語句中正確聲明了數據庫字段名稱。 試圖找出我收到此錯誤的原因。 真的需要一些幫助..謝謝

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)

編輯:字段相同,這是一個屏幕截圖 在此輸入圖像描述

我沒有訪問自己所以我不能嘗試這個,但這里有可能工作的東西。 首先,我們打印出結果集中列的名稱,以防在工作時出現一些區分大小寫。

然后我們使用位置參數(1,2,...)代替名稱在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);
 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM