我已经在这几个小时了,并且一直在谷歌上搜索,我似乎无法解决它。
当我运行代码时,我得到一个异常,堆栈跟踪的顶部告诉我我有一个sql语法错误,但我无法发现它。
我已经尝试了几种解决方案,但我仍在画空白,感谢任何帮助
由于 彼得
private String insertBookingKnownUser(Connection connection,
HttpServletRequest request) throws SQLException
{
try
{
String queryString = "INSERT INTO booking (hotelNo, guestNo, dateFrom, dateTo, roomNo) VALUES (?, ?, ?, ?, ?)";
statement = (PreparedStatement) connection.prepareStatement(queryString);
int hotelNumber = Integer.parseInt(request.getParameter("hotelNumber"));
//REMEMBER TO CHANGE GUESTNUMBER
int guestNumber = 1;
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date dateFrom;
dateFrom = (Date)formatter.parse(request.getParameter("dateFrom"));
java.sql.Date sqlDateFrom = new java.sql.Date(dateFrom.getTime());
Date dateTo = (Date)formatter.parse(request.getParameter("dateTo"));
java.sql.Date sqlDateTo = new java.sql.Date(dateTo.getTime());
int roomNumber= Integer.parseInt(request.getParameter("roomNumber"));
statement.setInt(1, hotelNumber);
statement.setInt(2, guestNumber);
statement.setDate(3, sqlDateFrom);
statement.setDate(4, sqlDateTo);
statement.setInt(5, roomNumber);
statement.executeUpdate(queryString);
}
catch (Exception e)
{
e.printStackTrace();
}
return "";
}
编辑:删除分号,仍然得到相同的错误
Stacktrace:
statementcom.mysql.jdbc.exceptions.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?, ?, ?, ?, ?)' at line 1
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:936)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2985)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1631)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1723)
at com.mysql.jdbc.Connection.execSQL(Connection.java:3277)
at com.mysql.jdbc.Statement.executeUpdate(Statement.java:1402)
at com.mysql.jdbc.Statement.executeUpdate(Statement.java:1317)
at SimpleServlet.insertBookingKnownUser(SimpleServlet.java:227)
at SimpleServlet.doGet(SimpleServlet.java:81)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:724)
答案 0 :(得分:2)
您必须致电statement.executeUpdate()
而不是statement.executeUpdate(queryString)
。
当您调用executeUpdate()
时,它会考虑您之前在语句中设置的参数,但是当您调用executeUpdate(String s)
时,它只会在没有设置参数的情况下执行给定的查询。