Insert Record JDBC Example

In the following program on Insert Record JDBC Example, 3 records are inserted in the Employee table in Oracle database.

Note: See that there exists an Employee table by the time this program is executed. This can be done with Create Table

Example on Insert Record JDBC Example
import java.sql.*;
public class InsertingRecords
{
  public static void main(String args[])  throws Exception
  {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

    Connection con = DriverManager.getConnection("jdbc:odbc:snrao", "scott", "tiger");

    Statement stmt = con.createStatement();
                                                    // inserting 3 records (of Insert Record JDBC Example)
    stmt.executeUpdate("insert into Employee values(100, ‘Sure Nag Rao’, 4500.50)");
    
    stmt.executeUpdate("insert into Employee values(200, ‘Sridhar’, 5500.50)");
    
    stmt.executeUpdate("insert into Employee values(300, ‘Jyostna ‘, 6500.50)");
        
    System.out.println("3 records inserted");  
    stmt.close();
    con.close();
  }
}  

In this code, the Employee data is hard coded (should be known at compile time itself). In the next program, the data is taken from keyboard and inserted.

Note 1: Before going into the program, it is advised to go through JDBC Programming Basic Steps where the meaning of Connection, Statement etc. are discussed.

Note 2: To know how to use try-catch-finally with the above code, example program is available at JDBC Example – Create Table.

Leave a Comment

Your email address will not be published.