Program to delete a record in a table using JDBC’s java.sql.Statement API.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
package com.c2j.jdbc.statement; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; public class JDBCDeleteRecordUsingStatement { public static void main(String[] args) throws SQLException { Connection con = null; Statement stmt = null; int rows=0; String sql = "DELETE EMPLOYEE WHERE ID=1"; try { con= getOracleDBConnection(); stmt = con.createStatement(); rows=stmt.executeUpdate(sql); } catch (SQLException e) { e.printStackTrace(); }finally { if (stmt != null) { stmt.close(); } if (con != null) { con.close(); } } System.out.println("Number of Rows deleted in EMPLOYEE table = "+rows); } private static Connection getOracleDBConnection(){ //Here TEST is oracle database name. String ORACLE_DB_CONNECTION = "jdbc:oracle:thin:@localhost:1521:TEST"; String ORACLE_DB_USER = "system"; String ORACLE_DB_PASSWORD = "system"; /* From JDBC 4.0(from Java6) onwards we no need to load data base driver * using class.forName(); Just add the database driver * jar(ojdbc6.jar for oracle) in the class path. * It will load when Drivermanager.getConnection method called. */ Connection oracleDbConnection = null; try { oracleDbConnection = DriverManager.getConnection (ORACLE_DB_CONNECTION, ORACLE_DB_USER,ORACLE_DB_PASSWORD); return oracleDbConnection; } catch (SQLException e) { e.printStackTrace(); } return oracleDbConnection; } } |
Output:
1 2 3 4 5 |
Number of Rows deleted in EMPLOYEE table = 1 |
In Database: