prepareStatement SQL错误 [英] preparedStatement SQL Error

查看:175
本文介绍了prepareStatement SQL错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

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;
}

但是我得到了

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

问题出在哪里?

推荐答案

KEY

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

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

在命名列名称时,避免使用reserved关键字始终是最好的解决方案.

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

这篇关于prepareStatement SQL错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆