Friday , July 21 2017
Home / jdbc / JDBC Insert Program Example

JDBC Insert Program Example

In this tutorials, 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.onlinetutorialspoint.jdbc; 
import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.SQLException; 
import java.sql.Statement; 
import java.util.Scanner; 
public class Jdbc_InsertionOperation_Example { 
    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(; 
            if (scanner != null) { 
                System.out.println("Enter Student No"); 
                studentNo = scanner.nextInt(); 
                System.out.println("Enter Student Name"); 
                studentName =; 
                System.out.println("Enter Student Address"); 
                studentAddress =; 
                System.out.println("Enter Student Age"); 
                studentAge = scanner.nextInt(); 
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/onlinetutorialspoint", "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) { 
        } catch (SQLException sqe) { 
        } catch (Exception e) { 
        } finally { 
            try { 
                if (statement != null) { 
            } catch (SQLException sqe) { 
            try { 
                if (connection != null) { 
            } catch (SQLException sqe) { 

Output :

Enter Student No
Enter Student Name
Enter Student Address
Enter Student Age
1 Record(s) Inserted

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+ ,


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 *