java SQLException: [Microsoft][ODBC Microsoft Access Driver] 参数太少。预计 1

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13300876/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 12:11:44  来源:igfitidea点击:

SQLException: [Microsoft][ODBC Microsoft Access Driver] Too few parameters. Expected 1

javaodbcdao

提问by Adesh

I am searching an access database for an item code and returning the item Object as shown in the code below. Any advise as to why I am getting this error will be appreciated. Thanks

我在访问数据库中搜索项目代码并返回项目对象,如下面的代码所示。任何有关为什么我收到此错误的建议将不胜感激。谢谢

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

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

Here's the error details :

这是错误详细信息:

java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Too few parameters. Expected 1.
    at sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6956)
    at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:7113)
    at sun.jdbc.odbc.JdbcOdbc.SQLExecDirect(JdbcOdbc.java:3109)
    at sun.jdbc.odbc.JdbcOdbcStatement.execute(JdbcOdbcStatement.java:337)
    at sun.jdbc.odbc.JdbcOdbcStatement.executeQuery(JdbcOdbcStatement.java:252)
    at checkoutsimulation.DAO.getIteminfo(DAO.java:49)
    at checkoutsimulation.ItemCatalog.getItemdetails(ItemCatalog.java:61)

采纳答案by nickles80

I'm not completely familiar with Java or ODBC but I do know that databases do not take kindly to column names with spaces.

我不完全熟悉 Java 或 ODBC,但我知道数据库不喜欢带空格的列名。

Try wrapping the column names in brackets.

尝试将列名括在方括号中。

itemDescription = results.getString("[Item Description]");

itemDescription = results.getString("[Item Description]");

回答by Surendra Jnawali

I was also faced same problem. finally solved nothing that, please check the db field name ItemCode(WHERE ItemCode = '"+itemCode+"'") with your database such error occurs when field name mistake on your SQL....

我也面临同样的问题。最终没有解决任何问题,请检查数据库字段名称 ItemCode(WHERE ItemCode = '"+itemCode+"'") 与您的数据库,当您的 SQL 上的字段名称错误时会发生此类错误....

回答by Rajdeep Bhuva

In This case you should use PreparedStatement

在这种情况下,您应该使用 PreparedStatement

PreparedStatement st = con.prepareStatement(""SELECT * FROM itemCatalog WHERE ItemCode=?");
st.setString(1,itemcode);
ResultSet rs = st.executeQuery();

回答by cedd

This can also happen if you reference a non existing field in the select part of the query. eg a query like the following.

如果您在查询的选择部分引用不存在的字段,也会发生这种情况。例如像下面这样的查询。

Select MisSpeltFieldName from tblStuff