Tuesday , February 28 2017
Home / jdbc / JDBC Update Program Example

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 :


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."); 
 
    } 
 
} 
1 Record(s) updated.
Happy Learning 🙂

About chandrashekhar

Hi Folks, you have reach this so far, that shows you like what you are learning. Then why don't you support us to improve for bettor tutorials by leaving your valuable comments and why not you keep in touch with us for latest updates on your favorite blog @ facebook , twitter , Or Google+ ,

Check Also

JDBC Updatable ResultSet Example

Whenever we create a ResultSet object which never allows us to update the database through …

Leave a Reply

Your email address will not be published. Required fields are marked *