简体   繁体   中英

Problem with Update query in Mysql with JDBC

  1. see the screenshots
  2. see the 2nd screenshot
  3. see the 3rd screenshot

Okay so I am building a project on java and mysql, I am stuck at this point that I have to update a data which is in MySql but from my java gui application, I've executed that update command from MySql command line client

update user set bldu = 50 where userid = 1001;

and it's working perfectly fine there but from my java application on clicking on assigned jbutton it says:

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 'userid= 1001' at line 1

Please help me..!

String query = "update user SET bldu = " + bldut + " WHERE userid = " + uid + ";";

使用此查询代替您的旧查询可能会对您有所帮助。

In your first screenshot you must add a space before WHERE clause:

String query = "UPDATE user SET bdlu = " + bldut + "WHERE userid = " + uid + ";";

So your query will be interpretated as:

UPDATE user SET bdlu = 50WHERE userid = 1001

So you'll raise a syntax error.

Then you'll have the following query:

String query = "UPDATE user SET bdlu = " + bldut + " WHERE userid = " + uid + ";";

Try this snippet in your code.

String query = "update user SET bldu = " + bldut + " WHERE userid = " + uid + ";";
Statement = con.prepareStatement(query);
Statement.executeUpdate();

by looking at your code you cannot store results of update query in resultSet the executeUpdate() only return 0 or 1 for success and failure of Update.

Okay i guys i have figured out something that it is working i mean this program is updating the data stored in mysql from netbeans via jdbc but it won't stop showing that error message like:

"Can not issue data manipulation statements with executeQuery()"

everytime i click one that assigned jButton..! but i checked the database the value i want to change is being changed but then why it is showing this error..?

Please use this code in your java file, do changes according to your file. your issue is you are using the same query in a result set that already uses for the update

Connection conn = DriverManager.getConnection( "jdbc:mysql://localhost:3306/bdb", "root", "root");

try {

    String query = "update user SET bldu = " + bldut+ " WHERE userid = " + uid + ";";           
    // create the java mysql update preparedstatement
    Class.forName("com.mysql.jdbc.Driver").newInstance();

    PreparedStatement preparedStmt = conn.prepareStatement(query);
    preparedStmt.executeUpdate();

    query = "select * from user  WHERE userid = " + uid +";";
    ResultSet rs = stmt.executeQuery(query);
    // STEP 5: Extract data from result set
    while (rs.next()) {
        // Retrieve by column name
        String userid = rs.getString("userid");
        String userfname = rs.getString("userfname");
        // all your column
        // Display values
        System.out.print("userid: " + userid);
    }
    // STEP 6: Clean-up environment
    rs.close();
    stmt.close();
    conn.close();
} catch (Exception e) {
    System.err.println("Got an exception! ");
    System.err.println(e.getMessage());
} finally {
    // finally block used to close resources
    try {
        if (conn != null)
            conn.close();
    } catch (SQLException se) {
        se.printStackTrace();
    }// end finally try
}// end try

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM