JDBC Insert Example Program | JDBC executeUpdate() Example | OTP

PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
JAVAEXCEPTIONSCOLLECTIONSSWINGJDBC
EXCEPTIONS
COLLECTIONS
SWING
JDBC
JAVA 8
SPRING
SPRING BOOT
HIBERNATE
PYTHON
PHP
JQUERY
PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
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.
executepdate() method returns the integer value. The value represents that the number of rows effected in the database.
int rowsEffected = stmt.executepUpdate("insert command");
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");
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", "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(); 
 
            } 
 
        } 
 
    } 
} 

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

Insert an Image using JDBC in Mysql DB
JDBC Select Program Example
JDBC Update Program Example
JDBC Delete Program Example
JDBC PreparedStatement Example Program
Step by Step JDBC Program Example
Read an Image in JDBC Example
CallableStatement in jdbc Example
ResultSetMetaData in JDBC Example
DatabaseMetaData in JDBC Example
Transaction Management in JDBC Example
Batch Processing in JDBC Example
JDBC Connection with Properties file
JDBC Scrollable ResultSet Example
JDBC Updatable ResultSet Example

Insert an Image using JDBC in Mysql DB
JDBC Select Program Example
JDBC Update Program Example
JDBC Delete Program Example
JDBC PreparedStatement Example Program
Step by Step JDBC Program Example
Read an Image in JDBC Example
CallableStatement in jdbc Example
ResultSetMetaData in JDBC Example
DatabaseMetaData in JDBC Example
Transaction Management in JDBC Example
Batch Processing in JDBC Example
JDBC Connection with Properties file
JDBC Scrollable ResultSet Example
JDBC Updatable ResultSet Example
 


Δ
 JDBC Driver Types
 Step by Step JDBC Program
 JDBC Select Program
 JDBC Insert Program
 JDBC Update Program
 JDBC Delete Program
 JDBC Connection – Properties File
 JDBC PreparedStatement Program
 JDBC – CallableStatement Example
 JDBC – Read an Image from DB
 JDBC – Insert an Image in DB
 JDBC – Updatable ResultSet
 JDBC – Scrollable ResultSet
 JDBC – ResultSetMetaData
 JDBC – DatabaseMetaData
 JDBC – Transaction Management
 JDBC – Batch Processing
 JDBC Interview Questions