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

JDBC Programs

The document is a Java program for managing a student database using JDBC. It allows users to insert, update, delete, and view student records through a console interface. The program connects to a MySQL database and executes SQL commands based on user input.

Uploaded by

ketandhadve95
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

JDBC Programs

The document is a Java program for managing a student database using JDBC. It allows users to insert, update, delete, and view student records through a console interface. The program connects to a MySQL database and executes SQL commands based on user input.

Uploaded by

ketandhadve95
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

import java.sql.

*;

import java.util.Scanner;

public class StudentDatabase {

private static final String URL = "jdbc:mysql://localhost:3306/your_database";

private static final String USER = "your_username";

private static final String PASSWORD = "your_password";

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

int choice;

try (Connection conn = DriverManager.getConnection(URL, USER, PASSWORD)) {

while (true) {

System.out.println("\n--- Student Database Management ---");

System.out.println("1. Insert Student");

System.out.println("2. Update Student");

System.out.println("3. Delete Student");

System.out.println("4. View Students");

System.out.println("5. Exit");

System.out.print("Enter your choice: ");

choice = scanner.nextInt();

scanner.nextLine();

switch (choice) {

case 1 -> insertStudent(conn, scanner);

case 2 -> updateStudent(conn, scanner);

case 3 -> deleteStudent(conn, scanner);

case 4 -> viewStudents(conn);

case 5 -> {

System.out.println("Exiting...");
return;

default -> System.out.println("Invalid choice! Try again.");

} catch (SQLException e) {

e.printStackTrace();

private static void insertStudent(Connection conn, Scanner scanner) throws SQLException {

System.out.print("Enter Name: ");

String name = scanner.nextLine();

System.out.print("Enter Age: ");

int age = scanner.nextInt();

scanner.nextLine();

System.out.print("Enter Grade: ");

String grade = scanner.nextLine();

String sql = "INSERT INTO student (name, age, grade) VALUES (?, ?, ?)";

try (PreparedStatement pstmt = conn.prepareStatement(sql)) {

pstmt.setString(1, name);

pstmt.setInt(2, age);

pstmt.setString(3, grade);

int rows = pstmt.executeUpdate();

System.out.println(rows + " student(s) inserted.");

private static void updateStudent(Connection conn, Scanner scanner) throws SQLException {

System.out.print("Enter Student ID to Update: ");


int id = scanner.nextInt();

scanner.nextLine();

System.out.print("Enter New Name: ");

String name = scanner.nextLine();

System.out.print("Enter New Age: ");

int age = scanner.nextInt();

scanner.nextLine();

System.out.print("Enter New Grade: ");

String grade = scanner.nextLine();

String sql = "UPDATE student SET name = ?, age = ?, grade = ? WHERE id = ?";

try (PreparedStatement pstmt = conn.prepareStatement(sql)) {

pstmt.setString(1, name);

pstmt.setInt(2, age);

pstmt.setString(3, grade);

pstmt.setInt(4, id);

int rows = pstmt.executeUpdate();

System.out.println(rows + " student(s) updated.");

private static void deleteStudent(Connection conn, Scanner scanner) throws SQLException {

System.out.print("Enter Student ID to Delete: ");

int id = scanner.nextInt();

String sql = "DELETE FROM student WHERE id = ?";

try (PreparedStatement pstmt = conn.prepareStatement(sql)) {

pstmt.setInt(1, id);

int rows = pstmt.executeUpdate();

System.out.println(rows + " student(s) deleted.");

}
}

private static void viewStudents(Connection conn) throws SQLException {

String sql = "SELECT * FROM student";

try (Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(sql)) {

System.out.println("\n--- Student Records ---");

while (rs.next()) {

System.out.println("ID: " + rs.getInt("id") +

", Name: " + rs.getString("name") +

", Age: " + rs.getInt("age") +

", Grade: " + rs.getString("grade"));

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy