谢谢Alexandre,但现在我一直收到以下错误:
java.sql.SQLException:[Microsoft] [ODBC Microsoft Access Driver]查询值和目标字段的数量不同。
但我有11个问号和11个参数,所以我也不理解这个错误。
PreparedStatement stmt = conn.prepareStatement("INSERT INTO STAFFDIR VALUES (?,?,?,?,?,?,?,?,?,?,?)");
stmt.setString(1, LN);
stmt.setString(2, FN);
stmt.setString(3, M);
stmt.setString(4, Phone);
stmt.setInt(5, DC);
stmt.setString(6, Email);
stmt.setInt(7, FC);
stmt.setString(8, Title);
stmt.setInt(9, LC);
stmt.setString(10, Picture);
stmt.setString(11, PC);
stmt.executeUpdate();
------------------原帖----------------------
以下代码会产生以下错误。为什么?这是MS Access 2010 .accdb数据库。有问题的表有另一个名为ID的字段 它被定义为主键。但我认为不需要以任何方式引用它。我呢?任何和所有的帮助表示赞赏,谢谢。
[5/8/14 10:06:34:986 EDT] 00000025 SystemOut O First Name = John
[5/8/14 10:06:34:986 EDT] 00000025 SystemOut O Last Name = Doe
[5/8/14 10:06:34:986 EDT] 00000025 SystemOut O Middle = M
[5/8/14 10:06:34:986 EDT] 00000025 SystemOut O Phone = 212 999-9999
[5/8/14 10:06:34:986 EDT] 00000025 SystemOut O DeptCode = 0
[5/8/14 10:06:34:986 EDT] 00000025 SystemOut O Email = nbroat@msn.com
[5/8/14 10:06:34:986 EDT] 00000025 SystemOut O Title = None
[5/8/14 10:06:34:986 EDT] 00000025 SystemOut O FaxCode = 2
[5/8/14 10:06:34:986 EDT] 00000025 SystemOut O LocCode = 1
[5/8/14 10:06:34:986 EDT] 00000025 SystemOut O Picture = none
[5/8/14 10:06:34:986 EDT] 00000025 SystemOut O Pic_Code = N
[5/8/14 10:06:35:047 EDT] 00000025 SystemOut O Error occurred java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Too few parameters. Expected 11.
[5/8/14 10:06:35:047 EDT] 00000025 SystemErr R java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Too few parameters. Expected 11.
[5/8/14 10:06:35:047 EDT] 00000025 SystemErr R at sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6969)
[5/8/14 10:06:35:047 EDT] 00000025 SystemErr R at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:7126)
[5/8/14 10:06:35:047 EDT] 00000025 SystemErr R at sun.jdbc.odbc.JdbcOdbc.SQLExecute(JdbcOdbc.java:3161)
[5/8/14 10:06:35:047 EDT] 00000025 SystemErr R at sun.jdbc.odbc.JdbcOdbcPreparedStatement.execute(JdbcOdbcPreparedStatement.java:228)
[5/8/14 10:06:35:047 EDT] 00000025 SystemErr R at sun.jdbc.odbc.JdbcOdbcPreparedStatement.executeQuery(JdbcOdbcPreparedStatement.java:103)
[5/8/14 10:06:35:047 EDT] 00000025 SystemErr R at cophonebk.COaddstaff.performTask(COaddstaff.java:104)
[5/8/14 10:06:35:047 EDT] 00000025 SystemErr R at cophonebk.COaddstaff.doPost(COaddstaff.java:39)
[5/8/14 10:06:35:047 EDT] 00000025 SystemErr R at javax.servlet.http.HttpServlet.service(HttpServlet.java:738)
[5/8/14 10:06:35:047 EDT] 00000025 SystemErr R at javax.servlet.http.HttpServlet.service(HttpServlet.java:831)
[5/8/14 10:06:35:047 EDT] 00000025 SystemErr R at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1663)
[5/8/14 10:06:35:047 EDT] 00000025 SystemErr R at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:939)
[5/8/14 10:06:35:047 EDT] 00000025 SystemErr R at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:502)
[5/8/14 10:06:35:047 EDT] 00000025 SystemErr R at com.ibm.ws.webcontainer.servlet.ServletWrapperImpl.handleRequest(ServletWrapperImpl.java:181)
[5/8/14 10:06:35:047 EDT] 00000025 SystemErr R at com.ibm.ws.webcontainer.webapp.WebApp.handleRequest(WebApp.java:3944)
[5/8/14 10:06:35:047 EDT] 00000025 SystemErr R at com.ibm.ws.webcontainer.webapp.WebGroup.handleRequest(WebGroup.java:276)
[5/8/14 10:06:35:047 EDT] 00000025 SystemErr R at com.ibm.ws.webcontainer.WebContainer.handleRequest(WebContainer.java:931)
[5/8/14 10:06:35:047 EDT] 00000025 SystemErr R at com.ibm.ws.webcontainer.WSWebContainer.handleRequest(WSWebContainer.java:1592)
[5/8/14 10:06:35:047 EDT] 00000025 SystemErr R at com.ibm.ws.webcontainer.channel.WCChannelLink.ready(WCChannelLink.java:186)
[5/8/14 10:06:35:047 EDT] 00000025 SystemErr R at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleDiscrimination(HttpInboundLink.java:452)
[5/8/14 10:06:35:047 EDT] 00000025 SystemErr R at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleNewRequest(HttpInboundLink.java:511)
[5/8/14 10:06:35:047 EDT] 00000025 SystemErr R at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.processRequest(HttpInboundLink.java:305)
[5/8/14 10:06:35:047 EDT] 00000025 SystemErr R at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.ready(HttpInboundLink.java:276)
[5/8/14 10:06:35:047 EDT] 00000025 SystemErr R at com.ibm.ws.tcp.channel.impl.NewConnectionInitialReadCallback.sendToDiscriminators(NewConnectionInitialReadCallback.java:214)
[5/8/14 10:06:35:047 EDT] 00000025 SystemErr R at com.ibm.ws.tcp.channel.impl.NewConnectionInitialReadCallback.complete(NewConnectionInitialReadCallback.java:113)
[5/8/14 10:06:35:047 EDT] 00000025 SystemErr R at com.ibm.ws.tcp.channel.impl.AioReadCompletionListener.futureCompleted(AioReadCompletionListener.java:165)
[5/8/14 10:06:35:047 EDT] 00000025 SystemErr R at com.ibm.io.async.AbstractAsyncFuture.invokeCallback(AbstractAsyncFuture.java:217)
[5/8/14 10:06:35:047 EDT] 00000025 SystemErr R at com.ibm.io.async.AsyncChannelFuture.fireCompletionActions(AsyncChannelFuture.java:161)
[5/8/14 10:06:35:047 EDT] 00000025 SystemErr R at com.ibm.io.async.AsyncFuture.completed(AsyncFuture.java:138)
[5/8/14 10:06:35:047 EDT] 00000025 SystemErr R at com.ibm.io.async.ResultHandler.complete(ResultHandler.java:204)
[5/8/14 10:06:35:047 EDT] 00000025 SystemErr R at com.ibm.io.async.ResultHandler.runEventProcessingLoop(ResultHandler.java:775)
[5/8/14 10:06:35:047 EDT] 00000025 SystemErr R at com.ibm.io.async.ResultHandler$2.run(ResultHandler.java:905)
[5/8/14 10:06:35:047 EDT] 00000025 SystemErr R at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1646)
Connection conn = null;
ResultSet result = null;
PreparedStatement stmt = null;
String LN = (String)request.getParameter("Last_Name");
String FN = (String)request.getParameter("First_Name");
String M = (String)request.getParameter("Middle");
String Ph = (String)request.getParameter("Phone");
String Dept_Code = (String)request.getParameter("Dept_Code");
Integer DC = Integer.valueOf(Dept_Code);
String E = (String)request.getParameter("Email");
String Fax_Code = (String)request.getParameter("Fax_Code");
Integer FC = Integer.valueOf(Fax_Code);
String T = (String)request.getParameter("Title");
String Loc_Code = (String)request.getParameter("Loc_Code");
Integer LC = Integer.valueOf(Loc_Code);
String Pic = (String)request.getParameter("Picture");
String PC = (String)request.getParameter("Pic_Code");
System.out.println("First Name = " + FN);
System.out.println("Last Name = " + LN);
System.out.println("Middle = " + M);
System.out.println("Phone = " + Ph);
System.out.println("DeptCode = " + DC);
System.out.println("Email = " + E);
System.out.println("Title = " + T);
System.out.println("FaxCode = " + FC);
System.out.println("LocCode = " + LC);
System.out.println("Picture = " + Pic);
System.out.println("Pic_Code = " + PC);
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn = DriverManager.getConnection("jdbc:odbc:CentralOfficePhoneDirectory");
try {
stmt = conn.prepareStatement("INSERT INTO STAFFDIR(Last_Name,First_Name,Middle,Phone,Dept_Code,Email,Title,Fax_Code,Loc_Code,Picture,Pic_Code) VALUES(LN,FN,M,Ph,DC,E,T,FC,LC,Pic,PC)");
result = stmt.executeQuery();
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
return;
} // End of try
catch (SQLException e) {
System.out.println("Error occurred " + e);
e.printStackTrace();
return;
}
答案 0 :(得分:0)
您需要将值传递给参数。错误是因为您没有提供任何参数。
在这里查看示例:http://docs.oracle.com/cd/F49540_01/DOC/java.815/a64686/05_dev4.htm
答案 1 :(得分:0)
试试这个:
PreparedStatement stmt = conn.prepareStatement("INSERT INTO STAFFDIR(Last_Name,First_Name,Middle,Phone,Dept_Code,Email,Title,Fax_Code,Loc_Code,Picture,Pic_Code) VALUES (?,?,?,?,?,?,?,?,?,?,?)");
stmt.setString(1, LN);
stmt.setString(2, FN);
stmt.setString(3, M);
stmt.setString(4, Phone);
stmt.setInt(5, DC);
stmt.setString(6, Email);
stmt.setInt(7, FC);
stmt.setString(8, Title);
stmt.setInt(9, LC);
stmt.setString(10, Picture);
stmt.setString(11, PC);
stmt.executeUpdate();