Skip to content

Commit 87c680f

Browse files
committedSep 18, 2024
First Commit
1 parent 620efb8 commit 87c680f

File tree

9 files changed

+328
-0
lines changed

9 files changed

+328
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-17">
4+
<attributes>
5+
<attribute name="module" value="true"/>
6+
</attributes>
7+
</classpathentry>
8+
<classpathentry kind="src" path="src"/>
9+
<classpathentry kind="lib" path="C:/Users/gajen/Downloads/mysql-connector-java-8.0.28.jar"/>
10+
<classpathentry kind="output" path="bin"/>
11+
</classpath>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/bin/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>MenuDrivenStudentManagement - JDBC</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.eclipse.jdt.core.javanature</nature>
16+
</natures>
17+
</projectDescription>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
eclipse.preferences.version=1
2+
encoding/<project>=UTF-8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=17
4+
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
5+
org.eclipse.jdt.core.compiler.compliance=17
6+
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
7+
org.eclipse.jdt.core.compiler.debug.localVariable=generate
8+
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
9+
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
10+
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
11+
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
12+
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
13+
org.eclipse.jdt.core.compiler.release=enabled
14+
org.eclipse.jdt.core.compiler.source=17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.StudentManagementCRUD;
2+
3+
import java.sql.Connection;
4+
import java.sql.DriverManager;
5+
import java.sql.SQLException;
6+
7+
public class DatabaseConnection {
8+
9+
private static final String URL = "jdbc:mysql://localhost:3306/StudentManagementDemo";
10+
private static final String USER = "root";
11+
private static final String PASSWORD = "Pass@123";
12+
13+
public static Connection getConnection() throws SQLException {
14+
return DriverManager.getConnection(URL, USER, PASSWORD);
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package com.StudentManagementCRUD;
2+
3+
import java.util.Scanner;
4+
5+
public class Main {
6+
7+
public static void main(String[] args) {
8+
Scanner sc = new Scanner(System.in);
9+
StudentOp op = new StudentOp();
10+
11+
while (true) {
12+
13+
System.out.println("----------------------Student Management----------------------");
14+
System.out.println("1 - Add Student ");
15+
System.out.println("2 - Display Student ");
16+
System.out.println("3 - Search Student ");
17+
System.out.println("4 - Remove Student ");
18+
System.out.println("5 - Update Student ");
19+
System.out.println("6 - Exit ");
20+
21+
System.out.print("\nEnter your choice : ");
22+
int choice = sc.nextInt();
23+
24+
switch (choice) {
25+
case 1:
26+
System.out.print("Enter Student Id : ");
27+
int studentId = sc.nextInt();
28+
29+
System.out.print("Enter Student Name :");
30+
String studentName = sc.next();
31+
32+
System.out.print("Enter Student Age : ");
33+
int studentAge = sc.nextInt();
34+
35+
System.out.print("Enter Student SGPA : ");
36+
double sgpa = sc.nextDouble();
37+
38+
System.out.print("Enter Student Phone Number : ");
39+
String phNumber = sc.next();
40+
41+
op.addStudent(studentId, studentName, studentAge, sgpa, phNumber);
42+
System.out.println("Student Successfully Added.");
43+
break;
44+
45+
case 2:
46+
System.out.println("All Students : ");
47+
op.displayStudents();
48+
break;
49+
50+
case 3:
51+
System.out.print("Enter Search Student Id : ");
52+
int searchId = sc.nextInt();
53+
op.findStudent(searchId);
54+
break;
55+
56+
case 4:
57+
System.out.println("Enter student Id to remove ");
58+
int removeId = sc.nextInt();
59+
op.removeStudnet(removeId);
60+
break;
61+
62+
case 5:
63+
System.out.print("Enter Student Id to update: ");
64+
int updateId = sc.nextInt();
65+
66+
System.out.print("Enter New Student Name: ");
67+
String newName = sc.next();
68+
69+
System.out.print("Enter New Student Age: ");
70+
int newAge = sc.nextInt();
71+
72+
System.out.print("Enter New Student CGPA: ");
73+
double newSgpa = sc.nextDouble();
74+
75+
System.out.print("Enter New Phone Number: ");
76+
String newPhoneNumber = sc.next();
77+
78+
op.updateStudent(updateId, newName, newAge, newSgpa, newPhoneNumber);
79+
break;
80+
81+
case 6:
82+
System.out.println("Exit Successfully");
83+
sc.close();
84+
System.exit(0);
85+
break;
86+
87+
default:
88+
System.out.println("Invalid choice, Please enter correct option");
89+
}
90+
91+
}
92+
}
93+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.StudentManagementCRUD;
2+
3+
public class Student {
4+
5+
private int studentId;
6+
private String studentName;
7+
private int studentAge;
8+
private double sgpa;
9+
private String studentPhNumber;
10+
11+
public Student() {
12+
}
13+
14+
public Student(int studentId, String studentName, int studentAge, double cgpa, String studentPhNumber) {
15+
super();
16+
this.studentId = studentId;
17+
this.studentName = studentName;
18+
this.studentAge = studentAge;
19+
this.sgpa = cgpa;
20+
this.studentPhNumber = studentPhNumber;
21+
}
22+
23+
public int getStudentId() {
24+
return studentId;
25+
}
26+
27+
public void setStudentId(int studentId) {
28+
this.studentId = studentId;
29+
}
30+
31+
public String getStudentName() {
32+
return studentName;
33+
}
34+
35+
public void setStudentName(String studentName) {
36+
this.studentName = studentName;
37+
}
38+
39+
public int getStudentAge() {
40+
return studentAge;
41+
}
42+
43+
public void setStudentAge(int studentAge) {
44+
this.studentAge = studentAge;
45+
}
46+
47+
public double getCgpa() {
48+
return sgpa;
49+
}
50+
51+
public void setCgpa(double cgpa) {
52+
this.sgpa = cgpa;
53+
}
54+
55+
public String getStudentPhNumber() {
56+
return studentPhNumber;
57+
}
58+
59+
public void setStudentPhNumber(String studentPhNumber) {
60+
this.studentPhNumber = studentPhNumber;
61+
}
62+
63+
@Override
64+
public String toString() {
65+
return "Student [studentId=" + studentId + ", studentName=" + studentName + ", studentAge=" + studentAge
66+
+ ", cgpa=" + sgpa + ", studentPhNumber=" + studentPhNumber + "]";
67+
}
68+
69+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package com.StudentManagementCRUD;
2+
3+
import java.sql.Connection;
4+
import java.sql.PreparedStatement;
5+
import java.sql.ResultSet;
6+
import java.sql.SQLException;
7+
import java.sql.Statement;
8+
9+
public class StudentOp {
10+
11+
public void addStudent(int studentId, String studentName, int studentAge, double sgpa, String phNumber) {
12+
try (Connection conn = DatabaseConnection.getConnection()) {
13+
String query = "INSERT INTO Students (studentId, studentName, studentAge, sgpa, phNumber) VALUES (?, ?, ?, ?, ?)";
14+
PreparedStatement stmt = conn.prepareStatement(query);
15+
stmt.setInt(1, studentId);
16+
stmt.setString(2, studentName);
17+
stmt.setInt(3, studentAge);
18+
stmt.setDouble(4, sgpa);
19+
stmt.setString(5, phNumber);
20+
21+
stmt.executeUpdate();
22+
} catch (SQLException e) {
23+
e.printStackTrace();
24+
}
25+
}
26+
27+
public void displayStudents() {
28+
try (Connection conn = DatabaseConnection.getConnection()) {
29+
String query = "SELECT * FROM Students";
30+
Statement stmt = conn.createStatement();
31+
ResultSet rs = stmt.executeQuery(query);
32+
33+
while (rs.next()) {
34+
System.out.println("ID: " + rs.getInt("studentId"));
35+
System.out.println("Name: " + rs.getString("studentName"));
36+
System.out.println("Age: " + rs.getInt("studentAge"));
37+
System.out.println("CGPA: " + rs.getDouble("sgpa"));
38+
System.out.println("Phone Number: " + rs.getString("phNumber"));
39+
System.out.println("-------------------------");
40+
}
41+
} catch (SQLException e) {
42+
e.printStackTrace();
43+
}
44+
}
45+
46+
public void findStudent(int searchId) {
47+
try (Connection conn = DatabaseConnection.getConnection()) {
48+
String query = "SELECT * FROM Students WHERE studentId = ?";
49+
PreparedStatement stmt = conn.prepareStatement(query);
50+
stmt.setInt(1, searchId);
51+
52+
ResultSet rs = stmt.executeQuery();
53+
if (rs.next()) {
54+
System.out.println("ID: " + rs.getInt("studentId"));
55+
System.out.println("Name: " + rs.getString("studentName"));
56+
System.out.println("Age: " + rs.getInt("studentAge"));
57+
System.out.println("CGPA: " + rs.getDouble("sgpa"));
58+
System.out.println("Phone Number: " + rs.getString("phNumber"));
59+
} else {
60+
System.out.println("Student not found");
61+
}
62+
} catch (SQLException e) {
63+
e.printStackTrace();
64+
}
65+
}
66+
67+
public void removeStudnet(int removeId) {
68+
try (Connection conn = DatabaseConnection.getConnection()) {
69+
String query = "DELETE FROM Students WHERE studentId = ?";
70+
PreparedStatement stmt = conn.prepareStatement(query);
71+
stmt.setInt(1, removeId);
72+
73+
int rowsAffected = stmt.executeUpdate();
74+
if (rowsAffected > 0) {
75+
System.out.println("Student removed successfully.");
76+
} else {
77+
System.out.println("Student not found.");
78+
}
79+
} catch (SQLException e) {
80+
e.printStackTrace();
81+
}
82+
}
83+
84+
public void updateStudent(int studentId, String newName, int newAge, double newSgpa, String newPhoneNumber) {
85+
try (Connection conn = DatabaseConnection.getConnection()) {
86+
String query = "UPDATE Students SET studentName = ?, studentAge = ?, sgpa = ?, phNumber = ? WHERE studentId = ?";
87+
PreparedStatement stmt = conn.prepareStatement(query);
88+
89+
stmt.setString(1, newName); // New Name
90+
stmt.setInt(2, newAge); // New Age
91+
stmt.setDouble(3, newSgpa); // New SGPA
92+
stmt.setString(4, newPhoneNumber); // New Phone Number
93+
stmt.setInt(5, studentId); // Where studentId matches
94+
95+
int rowsAffected = stmt.executeUpdate();
96+
if (rowsAffected > 0) {
97+
System.out.println("Student details updated successfully.");
98+
} else {
99+
System.out.println("Student not found.");
100+
}
101+
} catch (SQLException e) {
102+
e.printStackTrace();
103+
}
104+
}
105+
}

0 commit comments

Comments
 (0)
Please sign in to comment.