preparedStatement SQL Error

社会主义新天地 提交于 2019-12-12 19:17:52

问题


Here is my code:

public int checklogin(String email, String key){
int res = -1;
try
{
    String selectSQL = "SELECT id FROM table WHERE email = ? AND key = ?";
    PreparedStatement preparedStatement = dbConnection.prepareStatement(selectSQL);
    preparedStatement.setString(1, email);
    preparedStatement.setString(2, key);

    ResultSet rs = preparedStatement.executeQuery();
    if (rs.next()) 
        res = rs.getInt("id");

    rs.close();   
    preparedStatement.close();          
} catch (Exception e) { e.printStackTrace(); }

return res;
}

But i get:

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 'key = 'AAA'' at line 1

Where is the problem?


回答1:


KEY is a reserved word in MySQL. It needs to be escaped

String selectSQL = "SELECT id FROM table WHERE email = ? AND `key` = ?";

Avoiding the reserved keywords is always the best solution when naming column names.



来源:https://stackoverflow.com/questions/21232923/preparedstatement-sql-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!