8. Create a student database and store the details of the students in a table.
Perform the
SELECT, INSERT,UPDATE and DELETE operations using JDBC connectivity.
Create Database
CREATE DATABASE StudentDB;
-- Use Database
USE StudentDB;
-- Create Table
CREATE TABLE students (
id INT PRIMARY KEY,
name VARCHAR(100),
age INT,
course VARCHAR(100)
);
Java program:
import java.sql.*;
import java.util.Scanner;
public class StudentDatabaseApp
// JDBC URL, username, password
static final String JDBC_URL = "jdbc:mysql://localhost:3306/StudentDB";
static final String USER = "root"; // your MySQL username
static final String PASS = "password"; // your MySQL password
public static void main(String[] args)
Scanner sc = new Scanner(System.in);
try
// 1. Load the JDBC Driver
Class.forName("com.mysql.cj.jdbc.Driver");
// 2. Establish the Connection
Connection conn = DriverManager.getConnection(JDBC_URL, USER, PASS);
while (true)
System.out.println("\nSelect an operation:");
System.out.println("1. Insert Student");
System.out.println("2. Update Student");
System.out.println("3. Delete Student");
System.out.println("4. Display Students");
System.out.println("5. Exit");
System.out.print("Choice: ");
int choice = sc.nextInt();
switch (choice)
case 1:
insertStudent(conn, sc);
break;
case 2:
updateStudent(conn, sc);
break;
case 3:
deleteStudent(conn, sc);
break;
case 4:
displayStudents(conn);
break;
case 5:
conn.close();
System.out.println("Thank you! Exiting...");
System.exit(0);
default:
System.out.println("Invalid choice!");
} catch (Exception e)
e.printStackTrace();
private static void insertStudent(Connection conn, Scanner sc) throws SQLException
System.out.print("Enter Student ID: ");
int id = sc.nextInt();
sc.nextLine(); // consume newline
System.out.print("Enter Student Name: ");
String name = sc.nextLine();
System.out.print("Enter Student Age: ");
int age = sc.nextInt();
sc.nextLine(); // consume newline
System.out.print("Enter Student Course: ");
String course = sc.nextLine();
String sql = "INSERT INTO students (id, name, age, course) VALUES (?, ?, ?, ?)";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, id);
pstmt.setString(2, name);
pstmt.setInt(3, age);
pstmt.setString(4, course);
int rows = pstmt.executeUpdate();
System.out.println(rows + " student(s) inserted.");
private static void updateStudent(Connection conn, Scanner sc) throws SQLException
System.out.print("Enter Student ID to Update: ");
int id = sc.nextInt();
sc.nextLine(); // consume newline
System.out.print("Enter New Course Name: ");
String newCourse = sc.nextLine();
String sql = "UPDATE students SET course = ? WHERE id = ?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, newCourse);
pstmt.setInt(2, id);
int rows = pstmt.executeUpdate();
System.out.println(rows + " student(s) updated.");
private static void deleteStudent(Connection conn, Scanner sc) throws SQLException
{
System.out.print("Enter Student ID to Delete: ");
int id = sc.nextInt();
String sql = "DELETE FROM students WHERE id = ?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, id);
int rows = pstmt.executeUpdate();
System.out.println(rows + " student(s) deleted.");
private static void displayStudents(Connection conn) throws SQLException
String sql = "SELECT * FROM students";
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
System.out.println("\nStudent Records:");
while (rs.next())
System.out.println("ID: " + rs.getInt("id") +
", Name: " + rs.getString("name") +
", Age: " + rs.getInt("age") +
", Course: " + rs.getString("course"));
Output:
ID Name Age Course
1001 Amit 20 B.Tech CSE
1002 Ram 21 B.Tech CSE