Delete records from database using JDBC

By Satyapriya Nayak Sep 24, 2011
In this blog we will know how to delete records from database using JDBC (Java Database Connectivity) in console window.
Download Files: Delete.rar

In this blog we will know how to delete records from database using JDBC (Java Database Connectivity) in console window.

 

Here we use Type-1 driver (JDBC-ODBC bridge)

 

Creation of dsn(database source name) for Oracle

 

Start-Control panel- Administrative Tools- Data Sources (ODBC)-go to system dsn tab-click add button-select a driver for which you want to set up data source (for Oracle- Oracle in XE)-select it and click finish-give any name in data source name textbox-then click ok button.

 

Note: - Here Username=system, Password=pintu and Dsn name=dsn1

 

Table Creation

Create table employee (empno int,empname varchar(50),sal int)

 

Example:- To delete record from a table

 

/*To delete record from a table by using Statement*/

import java.sql.*;

import java.util.*;

public class delete

{

public static void main(String args[]) throws Exception

{

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

Scanner sc=new Scanner(System.in);

Connection con=DriverManager.getConnection("jdbc:odbc:dsn1","system","pintu");

Statement stmt=con.createStatement();

System.out.print("Enter lower limit salary: ");

int lb=sc.nextInt();

System.out.print("Enter upper limit salary: ");

int ub=sc.nextInt();

String sql="delete from employee where sal between "+lb+" and "+ub;

int no=stmt.executeUpdate(sql);

System.out.println(no +" Records Successfully Deleted...");

con.close();

}

}

Compile

Javac delete.java

Java delete

 

Satyapriya Nayak

Satyapriya Nayak is a software developer from Bhubaneswar, India. He holds a Bachelor's degree in Electronics and telecommunication and is an active member of C# Corner. 

COMMENT USING

PREMIUM SPONSORS

Hire Mobile & Web Developer on demand. 100% satisfaction. Try for 1 week or Money Back. Local and remote developers available all over USA.

Latest Blogs