gpt4 book ai didi

java.sql.SQLException : Column not found Error?

转载 作者:行者123 更新时间:2023-12-02 07:27:56 24 4
gpt4 key购买 nike

我正在使用以下代码查询 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)

已编辑:字段相同,这是屏幕截图 enter image description here

最佳答案

我自己没有 Access 权限,所以我无法尝试这个,但这里有一些可能有用的东西。首先,我们打印出结果集中的列名称,以防工作中存在大小写敏感性。

然后我们在 result.next 循环中使用位置参数 (1, 2, ..) 而不是名称进行解决。无论名称是什么,这都应该使其正常工作。

一旦弄清楚名称问题是什么,请将 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);
}

关于java.sql.SQLException : Column not found Error?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13301296/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com