我有一個如下所示的代碼。我嘗試了其他解決方案,但沒有任何東西似乎工作。生成錯誤的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
例外是如下
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)
你有什麼例外嗎?你可以發佈完整的堆棧跟蹤嗎? – 2013-02-11 14:15:11
請勿添加快照。在這裏複製併發布例外。我甚至看不到那個圖像。 – 2013-02-11 14:18:51
你可以右鍵單擊並選擇查看圖像 – 2013-02-11 14:19:24