DELHI TECHNICAL CAMPUS
Greater Noida
Affiliated to GGSIPV and Approved by AICTE & COA
EXPERIMENT 4
AIM: Create any Application Using any web application that performs Create, Insert,
Delete, Update Operations on database Using JDBC.
THEORY:
JDBC (Java Database Connectivity) is an API in Java used to connect and interact with
databases like MYSQL.
It allows execution of SQL queries such as CREATE, INSERT, UPDATE,
DELETE (CRUD operations).
Steps involved in JDBC:
1. Load JDBC Driver
2. Establish Connection
3. Create Statement
4. Execute Query
5. Process Result
6. Close Connection
CODE:
CREATE DATABASE CollegeDB;
USE CollegeDB;
CREATE TABLE Student (
Student1D INT PRIMARY KEY,
Name VARCHAR(50),
Age INT
import java .sql.*;
import [Link]
.Scanner;
public class {
static final String URL = "jdbc:mysqI://Iocalhost:3306/CollegeDB";
static final String USER = "root"; static final String PASS =
"password"; // change accordingly
DELHI TECHNICAL CAMPUS
Greater Noida
Affiliated to GGSIPV and Approved by AICTE & COA
public static void args) {
try {
Connection con = [Link](URL, USER, PASS);
Scanner sc = new Scanner([Link]);
while (true) {
[Link]("\nl . Insert\n2. Display\n3. Update\n4. Delete\n5. Exit");
int choice = [Link](); switch (choice) {
case 1: // INSERT
[Link]("Enter ID: t');
int id = [Link]();
[Link]();
[Link]("Enter Name: 'i);
String name = [Link]();
[Link]("Enter Age:
"); int age = [Link]();
PreparedStatement PSI = [Link](
"INSERT INTO Student VALUES 2,
PSI , id); PSI
.setString(2, name); ps
I setlnt(3, age); ps I
.executeUpdate();
System out .println( "Record Inserted !
break;
case 2: // DISPLAY
Statement st = [Link]();
ResultSet rs = * FROM Student");
Table:");
while ([Link]()) {
[Link](
[Link](l) + " +
[Link](2) + "
[Link](3)); break;
case 3: // UPDATE
DELHI TECHNICAL CAMPUS
Greater Noida
Affiliated to GGSIPV and Approved by AICTE & COA
[Link]("Enter ID to update: "); int
uid = [Link]();
[Link]("Enter new Age: i'); int
newAge = [Link]();
PreparedStatement ps2 = [Link](
"UPDATE Student SET Age=? WHERE StudentlD=?")•,
[Link]( I , newAge); [Link](2, uid);
[Link]();
System Updated!"); break;
case 4: // DELETE
ID to delete:
int did = [Link]();
PreparedStatement ps3 = [Link](
"DELETE FROM Student WHERE
[Link](I , did); ps3 .executeUpdate();
Deleted!"); break;
case 5:
[Link]();
[Link]( "Connection Closed ! ");
return;
default:
[Link]("lnvalid Choice!
} catch (Exception e) {
System .out .println(e);
OUTPUT :
I . Insert
2. Display
3. Update
4. Delete
5. Exit
DELHI TECHNICAL CAMPUS
Greater Noida
Affiliated to GGSIPV and Approved by AICTE & COA
Enter choice: I
Enter ID: 101
Enter Name: Aditi
Enter Age: 20
Record Inserted!
Enter choice: I
Enter ID: 102
Enter Name: Rahul
Enter Age: 21
Record Inserted!
Enter choice: 2
Student Table:
101 Aditi 20
102 Rahul 21
Enter choice: 3
Enter ID to update: 101
Enter new Age: 22
Record Updated!
Enter choice: 2
Student Table:
101 Aditi 22
102 Rahul 21
Enter choice: 4
Enter ID to delete: 102
Record Deleted!
Enter choice: 2
Student Table:
101 Aditi 22