Question

I have been going at this for several hours now, and have been googling around and I cant seem to resolve it.

When I run the code, I get an exception and the top of the stacktrace tells me I have an sql syntax error, but I can't spot it.

I have tried several solutions but I am still drawing blanks, any help is appreciated

Thanks Peter

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 "";
     }

edit: removed a semicolon, still get the same error

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)
Was it helpful?

Solution

You have to call statement.executeUpdate() instead of statement.executeUpdate(queryString).

When you call executeUpdate() it takes into account the arguments you set on the statement before but when you call executeUpdate(String s) it just executes the given query without the set parameters.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top