0

Java クラスを実行して mysql db を更新しているときに、このエラーが発生しました

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 'usage = 6000 where user_id = 1 and api_id = 1'
 at line 1
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstruct
orAccessorImpl.java:39)..

コードは次のとおりです。

int u_id=1;
int a_id=1;

String url = "jdbc:mysql://localhost:3306/new_db";
String driver = "com.mysql.jdbc.Driver";
String userName = "root";
String password = "1234";
int APIusage = 0;


try {
    Class.forName(driver).newInstance();

    Connection conn = DriverManager.getConnection(url, userName, password);
    String query = "update api_consume set usage = ? where user_id = ? and api_id = ?";
    PreparedStatement preparedStmt = conn.prepareStatement(query);
    preparedStmt.setInt(1, 6000);
    preparedStmt.setInt(2, u_id);
    preparedStmt.setInt(3, a_id);

    // execute the java preparedstatement
    preparedStmt.executeUpdate();

    conn.close();
} 
catch (Exception e) {
      e.printStackTrace();
}

構文エラーがある理由を確認するために、それを調べていただけますか? ありがとう

4

2 に答える 2

4

usageは MySQL の予約語であるため、引用する必要があります。

String query = "update api_consume set `usage` = ? where user_id = ? and api_id = ?";
于 2012-09-07T09:39:44.480 に答える
0

usageキーワードのせいです。その列名を usage_msg などに変更します。

于 2012-09-07T09:40:14.650 に答える