2012-09-07 59 views
0

我有這樣的錯誤在執行一個Java類更新MySQL數據庫java的mysql的更新

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

能否請你看看到它明白爲什麼有語法錯誤? 感謝

+0

問題是? – Thomas

回答

4

usage是MySQL中的reserved word,所以需要引用它:

String query = "update api_consume set `usage` = ? where user_id = ? and api_id = ?"; 
0

正是由於usage關鍵詞。將該列名稱更改爲usage_msg或其他內容。