-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServerConnection.java
124 lines (111 loc) · 4.67 KB
/
ServerConnection.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import java.sql.*;
public class ServerConnection {
public void createDB(String dataBase){
try {
String url = "jdbc:mysql://localhost:3306/";
String userName = "root";
String password = "Sidhu@1234";
Connection conn = DriverManager.getConnection(url, userName, password);
Statement stm = conn.createStatement();
String query = "CREATE database " + dataBase;
stm.execute(query);
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public void createTable(String tableName){
try {
String url = "jdbc:mysql://localhost:3306/project";
String userName = "root";
String password = "Sidhu@1234";
Connection conn = DriverManager.getConnection(url, userName, password);
Statement stm = conn.createStatement();
String query = "CREATE TABLE " + tableName + " (ClassId int NOT NULL AUTO_INCREMENT, FirstName varchar(20) NOT NULL, LastName varchar(20), Age int, PRIMARY KEY(ClassId))";
stm.execute(query);
System.out.println("Table created successfully.");
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public void createRecord(String firstName,String lastName, int age){
try {
String url = "jdbc:mysql://localhost:3306/";
String db = "project";
String userName = "root";
String password = "Sidhu@1234";
Connection conn = DriverManager.getConnection(url+db, userName, password);
String query = "INSERT INTO Student (FirstName, LastName, Age) VALUES (?,?,?)";
PreparedStatement pstm = conn.prepareStatement(query);
pstm.setString(1, firstName);
pstm.setString(2, lastName);
pstm.setInt(3, age);
pstm.execute();
System.out.println("Recird added successfully.");
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public void readTable() {
try {
String url = "jdbc:mysql://localhost:3306/";
String db = "project";
String userName = "root";
String password = "Sidhu@1234";
Connection conn = DriverManager.getConnection(url+db, userName, password);
Statement stm = conn.createStatement();
String query = "SELECT * FROM Student";
ResultSet rs = stm.executeQuery(query);
while(rs.next()){
System.out.println("Id -->"+rs.getInt(1));
System.out.println("First Name -->"+rs.getString(2));
System.out.println("Last Name -->"+rs.getString(3));
System.out.println("Age -->"+rs.getInt(4));
}
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
// public void updateRecord(){
public void updateRecord(String newName, int classID){
try {
String url = "jdbc:mysql://localhost:3306/";
String db = "project";
String userName = "root";
String password = "Sidhu@1234";
Connection conn = DriverManager.getConnection(url+db, userName, password);
String query = "UPDATE Student SET FirstName=? WHERE ClassId=?";
PreparedStatement pstm = conn.prepareStatement(query);
pstm.setString(1, newName);
pstm.setInt(2, classID);
pstm.execute();
System.out.println("Record updated successfully.");
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public void deleteRecord(String firstName, String lastName, int age) {
String url = "jdbc:mysql://localhost:3306/project";
String username = "root";
String password = "Sidhu@1234";
String sql = "DELETE FROM Student WHERE FirstName=? AND LastName=? AND Age=?";
try (Connection conn = DriverManager.getConnection(url, username, password);
PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setString(1, firstName);
pstmt.setString(2, lastName);
pstmt.setInt(3, age);
int rowsDeleted = pstmt.executeUpdate();
if (rowsDeleted > 0) {
System.out.println("Record deleted successfully");
} else {
System.out.println("No such record exists");
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}