Java JDBC SQL Update PreparedStatement a record - Java @ Desk

Wednesday, December 4, 2013

Java JDBC SQL Update PreparedStatement a record

Java JDBC SQL Update a record 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 = "UPDATE EMPLOYEE SET employeename = ? Where employeeid = ?");
  
                try {  
                     dbConnection = getDBConnection();  
                     dbConnection.setAutoCommit(false);  
                     preparedStatement = dbConnection.prepareStatement(insertQuery  
                               .toString());
       preparedStatement.setString(1, "Kumar Bhatia");
              preparedStatement.setInt(2, 10);
                     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;  
      }  
 }  






No comments:

Post a Comment