MySQLSyntaxErrorException:SQL语法中的错误

时间:2014-04-25 14:29:47

标签: java mysql sql jdbc

请查看以下代码

 public int insert(int index, String hashWord)
    {
        String str="";
        int result=0;
        int returnResult=0;

        try
        {
            con.setAutoCommit(false);
            PreparedStatement ps = con.prepareStatement("insert into Key_Word (key,hashed_word) values(?,?)");
            ps.setInt(1, index);
            ps.setString(2, hashWord);

            result = ps.executeUpdate();
            con.commit();

            if(result>0)
            {
                returnResult = 1;
            }
            else
            {
                returnResult = -1;
                System.out.println( index+" failed to update");
            }


        }
        catch(SQLException s)
        {
            returnResult = -1;
            s.printStackTrace();
            try {
                con.rollback();
                System.out.println(index+" Exception Occured. Update Failed");

            } catch (SQLException ex) {
                ex.printStackTrace();
                str = index+" Exception Occured. Update Failed. Rollback Failed";
            }
        }

        return returnResult;
    }

当我运行此代码时,它会给出以下错误。

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 'key,hashed_word) values(0,'001')' at line 1
0 Exception Occured. Update Failed
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
Operation Broke
Completed
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
    at com.mysql.jdbc.Util.getInstance(Util.java:386)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1054)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4237)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4169)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2617)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2778)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2834)
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2156)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2441)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2366)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2350)
    at inglesrapido.DatabaseHandler.insert(DatabaseHandler.java:64)
    at inglesrapido.KeyWordTableCreator.readAndUpdate(KeyWordTableCreator.java:53)
    at inglesrapido.KeyWordTableCreator.<init>(KeyWordTableCreator.java:25)
    at inglesrapido.Main.main(Main.java:26)

下面是我的MySQL表

enter image description here

这里有什么问题?

1 个答案:

答案 0 :(得分:9)

key是一个保留字,它需要转义才能在查询中使用,例如:

insert into Key_Word (`key`,hashed_word) values(?,?)

然而,选择另一个不会与sql关键字冲突的列名称会好得多。