2013-02-11 53 views
2

我有一個如下所示的代碼。我嘗試了其他解決方案,但沒有任何東西似乎工作。生成錯誤的Java代碼如下所示。MySQLSyntaxErrorException - 準備好的語句中的參數無效

public Connection getConnection() 
    { 
     try 
     { 
     conn = DriverManager.getConnection(CONNSTR, USER, PASS); 
     return conn; 
     } 
     catch (SQLException e) 
     { 
     e.printStackTrace(); 
     return null; 
     } 
    } 

    public boolean AuthenticateUser() 
    { 
     String UserName = "User"; 
     String Password = "Password"; 

     try 
     { 
      PreparedStatement pstmt = null; 
      ResultSet   rs = null; 
      conn = getConnection(); 

      String strSQL = "SELECT UserName " + 
         " FROM Users " + 
         " WHERE UserId = ? AND " + 
         "  Password = ?"; 

      pstmt = (PreparedStatement) conn.prepareStatement(strSQL); 
      pstmt.setString(1, UserName); 
      pstmt.setString(2, Password); 

      System.out.println(strSQL); 

      rs = pstmt.executeQuery(strSQL); 

      System.out.println(rs.getRow()); 

      rs.last(); 
      int TotalRows = rs.getRow(); 
      System.out.println(TotalRows); 

      if(TotalRows > 0) 
      return true; 
      else 
      return false; 

     } 
     catch (SQLException e) 
     { 
      e.printStackTrace(); 
      return false; 
     }   
    } 

數據庫的表結構下面給出

CREATE TABLE Users(Id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, 
        UserName VARCHAR(255), 
        UserId VARCHAR(255), 
       Password VARCHAR(255)) 

該錯誤是如下給出在Image enter image description here

例外是如下

com.mysql.jdbc.exceptions.jdbc4.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 '? AND   Password = ?' at line 1 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) 
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) 
    at java.lang.reflect.Constructor.newInstance(Unknown Source) 
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411) 
    at com.mysql.jdbc.Util.getInstance(Util.java:386) 
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1052) 
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3609) 
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3541) 
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2002) 
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2163) 
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2618) 
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2568) 
    at com.mysql.jdbc.StatementImpl.executeQuery(StatementImpl.java:1557) 
    at com.apryll.db.util.dbUtil.AuthenticateUser(dbUtil.java:55) 
    at com.apryll.db.Login.doPost(Login.java:42) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:641) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) 
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240) 
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164) 
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462) 
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164) 
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100) 
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562) 
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) 
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:395) 
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:250) 
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188) 
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) 
    at java.lang.Thread.run(Unknown Source) 
+0

你有什麼例外嗎?你可以發佈完整的堆棧跟蹤嗎? – 2013-02-11 14:15:11

+1

請勿添加快照。在這裏複製併發布例外。我甚至看不到那個圖像。 – 2013-02-11 14:18:51

+0

你可以右鍵單擊並選擇查看圖像 – 2013-02-11 14:19:24

回答

3

你再次通過SQL到executeQuery方法。該簽名不會出現在API中。該方法不需要參數。

executeQuery() javaDoc

+1

它出現在'Statement'(PreparedStatement'的超類)的API中,但驅動程序應該在'Statement'方法拋出一個異常接受一個查詢字符串是從'PreparedStatement'或'CallableStatement'中調用的。 – 2013-02-11 14:41:36

0

您將變量UserName作爲fi傳遞第一個查詢參數,但在查詢中使用UserId,它是一個int而不是一個String。

希望它能幫助,

+0

感謝您的回覆。不,我檢查它的字符串主字段保留爲Id – 2013-02-11 14:18:05

+0

請檢查表結構 – 2013-02-11 14:18:50