<分区>
当我尝试运行该程序时出现此异常
Exception Caughtcom.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 pass =?' at line 1
当我对值进行硬编码时,我得到了正确的结果。例如:SELECT name, pass from servlettutorial.logintable where name = 'Bill' and pass = 'Gates'
运行得很好。
上面程序中的 SQL 语法可能有什么问题?
try{
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/servlettutorial?"
+ "user=root&password=carinov");
String sql = "SELECT name, pass from servlettutorial.logintable where name = ? and pass = ?";
PreparedStatement stmt = conn.prepareStatement(sql);
stmt.setString(1, serv_name);
stmt.setString(2,serv_pass);
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
String d_name = rs.getString("name");
String d_pass = rs.getString("pass");
if(d_name.equals(serv_name) && (d_pass.equals(serv_pass)))
{
found_match=1;
break;
}
}