[go: up one dir, main page]

0% found this document useful (0 votes)
17 views6 pages

ADV JAVA Practical 1 KRUSHIL

The document outlines a JDBC desktop program for performing CRUD and search operations on a Student class in a MySQL database. It includes functionalities to add, view, edit, delete, and search for students based on their enrollment number. The program utilizes Java's SQL package for database connectivity and operations, with a user interface for interaction.

Uploaded by

Aniket Mali
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)
17 views6 pages

ADV JAVA Practical 1 KRUSHIL

The document outlines a JDBC desktop program for performing CRUD and search operations on a Student class in a MySQL database. It includes functionalities to add, view, edit, delete, and search for students based on their enrollment number. The program utilizes Java's SQL package for database connectivity and operations, with a user interface for interaction.

Uploaded by

Aniket Mali
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/ 6

ADVANCED JAVA PROGRAMMING 202045605

Practical 1
Write a JDBC desktop program to perform following CRUD and Search operation. Create
appropriate table in database to store objects of Student class.
1. Add Student. (Create)
2. View Students. (Display all students)
3. Edit Student. (Update)
4. Delete Student. (Delete)
5. Search Student (Find student based on enrolment
No) import java.sql.*;
import java.util.Scanner;
public class Pr1 {
public static void main(String[] args) {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/Krushil",
"root", "");
Statement st =
con.createStatement(); int choice,
rowsaffected;
Scanner ob = new
Scanner(System.in); do {
System.out.println("\n\n1. Add student");
System.out.println("2. View students");
System.out.println("3. Edit student");
System.out.println("4. Delete student");
System.out.println("5. Search student");
System.out.println("6. Exit");
System.out.print("Enter your choice: ");
choice = ob.nextInt();
System.out.println("\n");
switch (choice) {
case 1:
PreparedStatement pst = con
.prepareStatement("INSERT INTO student (id, name, gender) VALUES (?, ?,
?)");
System.out.print("\nEnter id: ");
int id = ob.nextInt();
System.out.print("\nEnter name:
"); String name = ob.next();
System.out.print("\nEnter gender: ");
String gender = ob.next();
pst.setInt(1, id);
pst.setString(2, name);
pst.setString(3, gender);
rowsaffected = pst.executeUpdate();
System.out.println(rowsaffected + " rows affected");
break;
12202040501012 1
ADVANCED JAVA PROGRAMMING 202045605

case 2:
ResultSet res = st.executeQuery("SELECT * FROM student");
while (res.next()) {
System.out.println(res.getInt(1) + " " + res.getString(2) + " " + res.getString(3));
}
break;
case 3:
System.out.print("Enter id of the student which you want to update: ");
int id1 = ob.nextInt();
System.out.print("\nEnter new name:
"); String name1 = ob.next();
System.out.print("\nEnter new gender:
"); String gender1 = ob.next();
PreparedStatement pst3 = con.prepareStatement("UPDATE student SET name=?,
gender=? WHERE id=?");
pst3.setString(1, name1);
pst3.setString(2, gender1);
pst3.setInt(3, id1);
rowsaffected = pst3.executeUpdate();
System.out.println(rowsaffected + " rows affected");
break;
case 4:
PreparedStatement pst1 = con.prepareStatement("DELETE FROM student WHERE
id=?");
System.out.print("\nEnter id of student which you want to delete: ");
int did = ob.nextInt();
pst1.setInt(1, did);
rowsaffected = pst1.executeUpdate();
System.out.println(rowsaffected + " rows affected");
break;
case 5:
PreparedStatement pst2 = con.prepareStatement("SELECT * FROM student
WHERE id=?");
System.out.print("\nEnter id of student which you want to search: ");
int sid = ob.nextInt();
pst2.setInt(1, sid);
ResultSet res1 =
pst2.executeQuery(); while
(res1.next()) {
System.out.println(res1.getInt(1) + " " + res1.getString(2) + " " +
res1.getString(3));
}
break;
default:
if (choice != 6) {
System.out.println("Invalid choice !!!");
}
break;
12202040501012 2
ADVANCED JAVA PROGRAMMING 202045605

}
} while (choice != 6);
ob.close();
st.close();
con.close();
} catch (Exception e)
{ e.printStackTrace(
);
}
}
Output:

}
12202040501012 3
ADVANCED JAVA PROGRAMMING 202045605

12202040501012 4
ADVANCED JAVA PROGRAMMING 202045605

12202040501012 5
ADVANCED JAVA PROGRAMMING 202045605

12202040501012 6

You might also like