JDBC CRUD ( Create, Read, Update and Delete) example

The following are login credentials :

  • Database Server - localhost
  • User - root
  • Password - root
  • Database - test

Step 1: Establish connection with database     [ Type1 driver ]         [ Type4 driver ]

Step 2: Create following table in MySQL database server -

use test;
create table Emp
(
code varchar(10) primary key,
name varchar(40) null,
city varchar(20),
salary int
);
insert into Emp values('a001','Ram Kumar','Noida',10000);
Download

Step3 : Following code to insert record in the above table -

public void insertEmp(String code, String name, String city, int sal) {
try {
    ps = con.prepareStatement("insert into Emp values(?,?,?,?)");
    ps.setString(1, code);
    ps.setString(2, name);
    ps.setString(3, city);
    ps.setInt(4, sal);
    int i = ps.executeUpdate();
    if (i != 0) {
        System.out.println("Inserted");
    else {
        System.out.println("not Inserted");
    }
catch (Exception e) {
    e.printStackTrace();
}
}

Step4 : Following source code is to update employee city and salary based on employee code -

public void updateEmp(String code, String city, int salary) {
try {
    ps = con.prepareStatement("update emp set city=?,salary=salary+? where code=?");
    ps.setString(1, city);
    ps.setInt(2, salary);
    ps.setString(3, code);
    int i = ps.executeUpdate();
    if (i != 0) {
        System.out.println("updated");
    else {
        System.out.println("not updated");
    }
catch (Exception e) {
    e.printStackTrace();
}
}

Step5 : Following source code is to delete an employee record based on employee code -

public void deleteEmp(String code) {
try {
    ps = con.prepareStatement("delete from emp where code=?");
    ps.setString(1, code);
    int i = ps.executeUpdate();
    if (i != 0) {
        System.out.println("deleted");
    else {
        System.out.println("not deleted");
    }
catch (Exception e) {
    e.printStackTrace();
}
}

Step6 : Following source code is to display an employee record based on employee code -

public void dispAnEmp(String s) {
try {
    ps = con.prepareStatement("select * from Emp where code=?");
    ps.setString(1, s);
    ResultSet res = ps.executeQuery();
    if (res.next()) {
        System.out.print(res.getString(1));
        System.out.print(res.getString(2));
        System.out.print(res.getString(3));
        System.out.println(res.getString(4));
    }
catch (SQLException e) {
    e.printStackTrace();
}
}

Step7 : Following source code is to display whole records from employee table -

public void dispAll() {
try {
    Statement st = con.createStatement();
    ResultSet res = st.executeQuery("select * from emp");
    while (res.next()) {
        System.out.print(res.getString(1));
        System.out.print(res.getString(2));
        System.out.print(res.getString(3));
        System.out.println(res.getString(4));
    }
catch (SQLException e) {
    e.printStackTrace();
}
Download Complete Source Code

Previous Next

Comment by : earcher for knowlege November 9th, 2013
Comment : thnx a lot it was very helpful for biginners like me ;)


Write your Comment

Name :
Email Id : [ We will not publish your email id ]
Your Comment : [ Maximum 200 chars ]
Enter Security code