0% found this document useful (0 votes)
2 views

jdbc

The document is a Java program that connects to a MySQL database to manage a 'teachers' table. It allows users to create the table, insert, display, update, and delete teacher records through a console menu. The program utilizes JDBC for database connectivity and includes error handling for invalid choices and missing records.

Uploaded by

Tanisha Waichal
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

jdbc

The document is a Java program that connects to a MySQL database to manage a 'teachers' table. It allows users to create the table, insert, display, update, and delete teacher records through a console menu. The program utilizes JDBC for database connectivity and includes error handling for invalid choices and missing records.

Uploaded by

Tanisha Waichal
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

package exam1;

import java.sql.*;
import java.util.Scanner;

public class jdbc22 {


public static void main(String[] args) throws SQLException,
ClassNotFoundException {
// Load MySQL JDBC Driver
Class.forName("com.mysql.cj.jdbc.Driver");

// Connect to MySQL database


Connection conn =
DriverManager.getConnection("jdbc:mysql://localhost:3306/data1", "root",
"System@123");
Scanner scanner = new Scanner(System.in);

while (true) {
System.out.println("\n1. Create Table\n2. Insert Data\n3. Display
Data\n4. Update Data\n5. Delete Data\n6. Exit");
System.out.print("Enter choice: ");
int choice = scanner.nextInt();

if (choice == 1) createTable(conn);
else if (choice == 2) insertData(conn, scanner);
else if (choice == 3) displayData(conn);
else if (choice == 4) updateData(conn, scanner);
else if (choice == 5) deleteData(conn, scanner);
else if (choice == 6) break;
else System.out.println("Invalid choice. Try again.");
}

// Close resources
conn.close();
scanner.close();
}

private static void createTable(Connection conn) throws SQLException {


String query = "CREATE TABLE IF NOT EXISTS teachers (TID INT PRIMARY
KEY, TName VARCHAR(100), Salary DOUBLE)";
conn.createStatement().executeUpdate(query);
System.out.println("Table created successfully.");
}

private static void insertData(Connection conn, Scanner scanner) throws


SQLException {
System.out.print("Enter ID: ");
int id = scanner.nextInt();
scanner.nextLine(); // Consume newline
System.out.print("Enter Name: ");
String name = scanner.nextLine();
System.out.print("Enter Salary: ");
double salary = scanner.nextDouble();

PreparedStatement pstmt = conn.prepareStatement("INSERT INTO teachers


VALUES (?, ?, ?)");
pstmt.setInt(1, id);
pstmt.setString(2, name);
pstmt.setDouble(3, salary);
pstmt.executeUpdate();

System.out.println("Data inserted successfully.");


}

private static void displayData(Connection conn) throws SQLException {


ResultSet rs = conn.createStatement().executeQuery("SELECT * FROM
teachers");
System.out.println("\nID | Name | Salary");
while (rs.next()) {
System.out.println(rs.getInt("TID") + " | " +
rs.getString("TName") + " | " + rs.getDouble("Salary"));
}
}

private static void updateData(Connection conn, Scanner scanner) throws


SQLException {
System.out.print("Enter ID of the teacher to update: ");
int id = scanner.nextInt();
scanner.nextLine(); // Consume newline
System.out.print("Enter new Name: ");
String name = scanner.nextLine();
System.out.print("Enter new Salary: ");
double salary = scanner.nextDouble();

PreparedStatement pstmt = conn.prepareStatement("UPDATE teachers SET


TName = ?, Salary = ? WHERE TID = ?");
pstmt.setString(1, name);
pstmt.setDouble(2, salary);
pstmt.setInt(3, id);

int rowsUpdated = pstmt.executeUpdate();


if (rowsUpdated > 0) {
System.out.println("Data updated successfully.");
} else {
System.out.println("No record found with the given ID.");
}
}

private static void deleteData(Connection conn, Scanner scanner) throws


SQLException {
System.out.print("Enter ID of the teacher to delete: ");
int id = scanner.nextInt();

PreparedStatement pstmt = conn.prepareStatement("DELETE FROM teachers


WHERE TID = ?");
pstmt.setInt(1, id);

int rowsDeleted = pstmt.executeUpdate();


if (rowsDeleted > 0) {
System.out.println("Data deleted successfully.");
} else {
System.out.println("No record found with the given ID.");
}
}
}
CREATE TABLE teachers (
TID INT PRIMARY KEY,
TName VARCHAR(100),
Salary DOUBLE

);

INSERT INTO teachers VALUES (1, ‘Alice’, 50000.0);


INSERT INTO teachers VALUES (2, ‘Bob’, 55000.0);
INSERT INTO teachers VALUES (3, ‘Charlie’, 60000.0);
INSERT INTO teachers VALUES (4, ‘David’, 65000.0);
INSERT INTO teachers VALUES (5, ‘Emma’, 70000.0);

You might also like