In this tutorials, we are going to understand a basic 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.executeUpdate("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.execute("insert command");

JDBC Insert Program Example:

Jdbc_InsertionOperation_Example.java
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(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/onlinetutorialspoint", "root", "password"); 
            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(); 
 
            } 
 
        } 
 
    } 
} 

Output :

Console
Enter Student No
2003
Enter Student Name
chandrashekhar
Enter Student Address
hyderabad
Enter Student Age
30
1 Record(s) Inserted

The above example is to insert a record into the database using executeUpdate() method from Statement class. This example is intended to show the basic understanding of the JDBC insert, but it’s not at all recommended to use in production grade systems.

Problems in above program:

1. JDBC Driver:

JdbcOdbcDriver is a type 1 driver it is not recommended for production grade systems. It is is simply a lightweight façade over another database protocol.

2. String interpolation:

If you take a look at our executeUpdate() method, we are passing an SQL insert query with interpolated string like below.

String insertQuery = "insert into student values('" + studentNo + "','" + studentName +"','"+ studentAddress + "','"+studentAge+"')";

Preparation a query using string concatenation like above is highly not recommended as it leads SQL Injection issue. To overcome this issue we can use the PreparedStatement approuch.

References:

Happy Learning 🙂