错误原因:mysql执行语句出错。

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

出错代码:

String sql = "select * from users where username = ? and password = ?";
ps = conn.prepareStatement(sql);
ps.setString(1, u.getUserName());
ps.setString(2, u.getPassWord());
rs = ps.executeQuery(sql);

出错原因:

查看帮助文档中的PreparedStatement类中的executeQuery方法描述

  • ()

    执行此 PreparedStatement对象中的SQL查询,并返回查询生成的 ResultSet对象。

可以看到此方法不需要传参,因此修改为:

String sql = "select * from users where username = ? and password = ?";
ps = conn.prepareStatement(sql);
ps.setString(1, u.getUserName());
ps.setString(2, u.getPassWord());
rs = ps.executeQuery();