Java JDBC SQL Insert example - Java @ Desk

Saturday, November 30, 2013

Java JDBC SQL Insert example

Java JDBC SQL Insert example
To insert a record in database in SQL using JDBC, you need to perform below mentioned steps:
1) Create the database connection. There are two ways to get the DB connection. One is through java.sql.DriverManager and another is through javax.sql.DataSource. Click on below links for specific configuration.
DriverManager DB connection
DataSource DB Connection

2) Create a PreparedStatement object using sql query:
PreparedStatement fielStatement = connection.prepareStatement(SQL_STRING);

3) Execute sql statement: This is done using execute() method of PreparedStatement object
fielStatement.execute();


4) Finally, close the PreparedStatement and Connection object

Complete example below
 package test;  
 import java.sql.Connection;  
 import java.sql.DriverManager;  
 import java.sql.PreparedStatement;  
 import java.sql.SQLException;  
 public class JdbcInsert {  
      public static void main(String[] argv) {  
           try {  
                Connection dbConnection = null;  
                PreparedStatement preparedStatement = null;  
                String insertQuery = "INSERT INTO EMPLOYEE (employeeid, employeename, createdby)"  
                          + " VALUES (1,'javacodeimpl','system')";  
                try {  
                     dbConnection = getDBConnection();  
                     dbConnection.setAutoCommit(false);  
                     preparedStatement = dbConnection.prepareStatement(insertQuery  
                               .toString());  
                     System.out.println(insertQuery);  
                     preparedStatement.execute();  
                     System.out.println("Record is inserted into EMPLOYEE table!");  
                     dbConnection.commit();  
                } catch (SQLException e) {  
                     dbConnection.rollback();  
                     System.out.println(e.getMessage());  
                } finally {  
                     if (preparedStatement != null) {  
                          preparedStatement.close();  
                     }  
                     if (dbConnection != null) {  
                          dbConnection.close();  
                     }  
                }  
           } catch (SQLException e) {  
                System.out.println(e.getMessage());  
           }  
      }  
      private static Connection getDBConnection() {  
           Connection dbConnection = null;  
           try {  
                Class.forName("oracle.jdbc.driver.OracleDriver");  
                dbConnection = DriverManager.getConnection(  
                          "jdbc:oracle:thin:@localhost:1521:javacodeimpl", "user",  
                          "password");  
                return dbConnection;  
           } catch (SQLException e) {  
                System.out.println(e.getMessage());  
           } catch (ClassNotFoundException e) {  
                System.out.println(e.getMessage());  
           }  
           return dbConnection;  
      }  
 }  






1 comment: