我编写了以下代码(摘要):
conn = Pooling.getDataSource().getConnection();
String databaseName = Configuration.getDatabaseName();
String sql = "SELECT * FROM " + databaseName + ".companies WHERE companyemail = ? AND companypassword = MD5(?)";
PreparedStatement prepStat = conn.prepareStatement(sql);
prepStat.setString(1, username);
prepStat.setString(2, password);
System.out.println("LoginService: prepStat = " + prepStat.toString());
ResultSet rs = prepStat.executeQuery(sql);
...
现在,当我执行此命令时,我得到一个
MySQLSyntaxErrorException
。 prepStat.toString()
打印:SELECT * FROM dbname.companies WHERE companyemail = '[email protected]' AND companypassword = MD5('passwort')
简单复制并粘贴到SequelPro即可成功返回结果。
但是,后端仍然声称语法有错误:
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 companypassword = MD5(?)' at line 1
Maybee我是盲人,但在这里看不到错误?但是这里发生了什么?
最佳答案
好的,我发现了问题所在。我用了:
ResultSet rs = prepStat.executeQuery(sql);
但是,我应该使用
ResultSet rs = prepStat.executeQuery();
代替。