为什么我收到错误语法错误?

时间:2014-08-31 18:22:35

标签: java mysql

Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/hibernate","root","root");
String  query1= "select * from registration where email= ? and password= ? ";
PreparedStatement ps = con.prepareStatement(query1);
ps.setString(1,email);
ps.setString(2,password);
ResultSet rs1= ps.executeQuery(query1);
rs1.next();
String s= rs1.getString("type");
out.println(s);

我收到以下错误 -

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 

1 个答案:

答案 0 :(得分:3)

ps.executeQuery(query1)

必须更改为

ps.executeQuery()

否则,您没有使用绑定参数执行准备好的查询,而是未准备好的查询,没有任何参数绑定。