0

I have this error while executing a java class to update a 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)..

here is the code:

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

Can you please have a look into it to see why there is a syntax error? thanks

0

2 Answers 2

4

usage is a reserved word in MySQL, thus you need to quote it:

String query = "update api_consume set `usage` = ? where user_id = ? and api_id = ?";
Sign up to request clarification or add additional context in comments.

Comments

0

It is because of usage key word. Change that column name to usage_msg or something.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.