JDBC

JDBC Update Program Example

In this tutorials, we are going to understand the JDBC update program example. To do the update operations in JDBC, the API given us two different methods executeUpdate(String qry) and execute(String qry).

By using the any one of those two, we can do the JDBC update program in Java. Here is the difference between the Jdbc executeUpdate() and execute().

execueteUpdate() Example :

executepdate() method returns the integer value. The value represents that the number of rows effected in the database.

int rowsEffected = stmt.executepUpdate("update command");

execuete() Example :

execute() method returns boolean value. As we already discussed in the JDBC select example, we can use the execute() method for both select and non-select operations. If the resultant object contains ResultSet then the execute() method returns the true, it returns false if it is an update count or no records found.

boolean isResultSet = stmt.executep("update command");

Here is the simple example Jdbc Update program.

JDBC Update Program Example :

Jdbc_Update_Example.java
package com.onlinetutorialspoint.jdbc; 
 
import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.Statement; 
 
public class Jdbc_Update_Example { 
 
    public static void main(String[] args) throws Exception { 
        Connection connection = null; 
 
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 
        connection = DriverManager.getConnection( 
                "jdbc:mysql://localhost:3306/onlinetutorialspoint", "root", 
                "123456"); 
 
        Statement stmt = connection.createStatement(); 
        int count = stmt 
                .executeUpdate("update student set sname='rajesh' where sid=102"); 
        System.out.println(count + " Record(s) updated."); 
 
    } 
 
}

Output:

Terminal
1 Record(s) updated.

Found a mistake or something out of date? Tell us →

Written by

chandrashekhar

Founder of onlinetutorialspoint.com. Enterprise architect with 15+ years in healthcare technology, designing and running systems on AWS and Azure. Writes about building real things with AI, local AI hardware and cloud certifications.

Keep reading

Ask a question or leave a comment