Return number of rows affected by SQL UPDATE statement in Java

后端 未结 7 1936
花落未央
花落未央 2020-12-30 20:26

I\'m using a MySQL database and accessing it through Java.

PreparedStatement prep1 = this.connection.prepareStatement(
        \"UPDATE user_table 
                


        
7条回答
  •  温柔的废话
    2020-12-30 20:34

    1. First of all, prepare the 'PreparedStatement' object using below constructor:

      PreparedStatement pStmt = con.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
      //here variable 'sql' is your query ("UPDATE user_table SET Level = 'Super' WHERE Username = ?") 
      
    2. Then, set your argument to 'pStmt'. In this case:

      prep1.setString(1, username);
      
    3. Finally, executeUpdate and get affected rows as an integer

      int affectedRows = pStmt.executeUpdate();
      

提交回复
热议问题