JDBC Insert Program Example

We are going to understand JDBC insert program. In the previous tutorial we have implemented JDBC select Program example, it is recommended to have a look if you don’t know.

To do the insert operation in JDBC, the api given us executeUpdate(String qry) and execute(String qry) methods. We can apply the both executeUpdate() aand execute() methods on statement object.

execueteUpdate() Example :

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


                        int rowsEffected = stmt.executepUpdate("insert 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("insert command");
                    
JDBC Insert Program Example :

                        package com.navabitsolutions.jdbc; 
 
                        import java.sql.Connection; 
                        import java.sql.DriverManager; 
                        import java.sql.SQLException; 
                        import java.sql.Statement; 
                        import java.util.Scanner; 
                         
                        public class JdbcInsertionOperationExample { 
                         
                            public static void main(String[] args) throws Exception { 
                         
                                Connection connection = null; 
                                Statement statement = null; 
                                Scanner scanner = null; 
                         
                                try { 
                                    int studentNo = 0; 
                                    String studentName = null; 
                                    String studentAddress = null; 
                                    int studentAge = 0; 
                                    scanner = new Scanner(System.in); 
                                    if (scanner != null) { 
                                        System.out.println("Enter Student No"); 
                                        studentNo = scanner.nextInt(); 
                                        System.out.println("Enter Student Name"); 
                                        studentName = scanner.next(); 
                                        System.out.println("Enter Student Address"); 
                                        studentAddress = scanner.next(); 
                                        System.out.println("Enter Student Age"); 
                                        studentAge = scanner.nextInt(); 
                                    } 
                                     
                                    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 
                                    connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/navabitsolutions", "root", "systemuser23!"); 
                                    if (connection != null) { 
                                        statement = connection.createStatement(); 
                                        String insertQuery = "insert into student values('" + studentNo + "','" + studentName +"','"
                                        + studentAddress + "','"+studentAge+"')"; 
                                        int result = statement.executeUpdate(insertQuery); 
                         
                                        if (result == 0) { 
                                            System.out.println("Record Inserted Failed"); 
                                        } else { 
                                            System.out.println(result+ " Record(s) Inserted "); 
                                        } 
                                    } 
                         
                                } catch (ClassNotFoundException cnfe) { 
                         
                                    cnfe.printStackTrace(); 
                                } catch (SQLException sqe) { 
                                    sqe.printStackTrace(); 
                         
                                } catch (Exception e) { 
                                    e.printStackTrace(); 
                                } finally { 
                                    try { 
                                        if (statement != null) { 
                                            statement.close(); 
                                        } 
                                    } catch (SQLException sqe) { 
                                        sqe.printStackTrace(); 
                         
                                    } 
                         
                                    try { 
                                        if (connection != null) { 
                                            connection.close(); 
                                        } 
                                    } catch (SQLException sqe) { 
                                        sqe.printStackTrace(); 
                         
                                    } 
                         
                                } 
                         
                            } 
                        }