Java JDBC SQL Delete example | Java @ Desk
Home » » Java JDBC SQL Delete example

Java JDBC SQL Delete example

Java JDBC SQL Delete example
To delete 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 JdbcDelete {  
      public static void main(String[] argv) {  
           try {  
                Connection dbConnection = null;  
                PreparedStatement preparedStatement = null;  
                String deleteQuery = "Delete From EMPLOYEE where employeeid = 101";  
                try {  
                     dbConnection = getDBConnection();  
                     dbConnection.setAutoCommit(false);  
                     preparedStatement = dbConnection.prepareStatement(deleteQuery  
                               .toString());  
                     System.out.println(deleteQuery);  
                     preparedStatement.execute();  
                     System.out.println("Record is deleted from 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;  
      }  
 }  




0 comments:

Post a Comment