即使使用Prepared Statement也会出现SQLSyntaxError

时间:2016-02-12 00:07:55

标签: java mysql jdbc syntax-error

我经常遇到SQL语法错误,但我无法理解我在哪里出错了。有人能指出我正确的方向吗?

String sql = "INSERT INTO Weights"
            + " (ID, W1, W2, W3, W4, W5)"
            + " VALUES (?, ?, ?, ?, ?, ?)";

     PreparedStatement statement = mysqlConnect.connect().prepareStatement( sql );
     statement.setString(1, String.valueOf( col-1 ) );
     statement.setDouble(2, weights[row][c]);
     statement.setDouble(3, weights[row][c]);
     statement.setDouble(4, weights[row][c]);
     statement.setDouble(5, weights[row][c]);
     statement.setDouble(6, weights[row][c]);
     statement.execute(sql);

1 个答案:

答案 0 :(得分:1)

您的sql语法已关闭,插入语句为。

INSERT INTO table (col1, col2, ...) VALUES(v1, v2, ...)

你基本上没有括号,改为跟随。

String sql = "INSERT INTO Weights"
        + "(ID, W1, W2, W3, W4, W5)"
        + " VALUES (?, ?, ?, ?, ?, ?)";