JDBC Update Program Example

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");
                    
JDBC Update Program Example :

                        package com.navabitsolutions.jdbc; 
 
                        import java.sql.Connection; 
                        import java.sql.DriverManager; 
                        import java.sql.Statement; 
                        
                        public class JdbcUpdateExample { 
                        
                            public static void main(String[] args) throws Exception { 
                                Connection connection = null; 
                        
                                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 
                                connection = DriverManager.getConnection( 
                                        "jdbc:mysql://localhost:3306/navabitsolutions", "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."); 
                        
                            } 
                        
                        }