-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJdbcConnection.java
163 lines (153 loc) · 8.43 KB
/
JdbcConnection.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import java.sql.*;
import java.util.Scanner;
public class JdbcConnection {
public static void main(String[] args) {
// Replace these with your database details
Scanner sc = new Scanner(System.in);
String jdbcUrl = "jdbc:mysql://localhost:3306/students";
String username = "root";
String password = "";
try {
// Establishing the database connection
Connection connection = DriverManager.getConnection(jdbcUrl, username, password);
// Driver Program
while (true) {
System.out.println(
"1. Add student details.\n2. Display student details\n3. Delete student details\n4. Update Student Details\n5. Join tables\n6. Find max marks\n7. Create view for marks greater than 90");
System.out.println("Please choose an option: ");
int choice = sc.nextInt();
sc.nextLine();
if (choice == 1) {
System.out.println("Please enter student's name: ");
String namee = sc.nextLine();
System.out.println("Please enter student's USN: ");
String usnn = sc.nextLine();
System.out.println("Please enter student's age: ");
int agee = sc.nextInt();
System.out.println("Please enter the year of birth:");
int yobb = sc.nextInt();
System.out.println("Please enter student's marks: ");
int markss = sc.nextInt();
String queryInsert = "INSERT INTO `students`(`Name`, `USN`, `Age`, `YOB`, `Marks`) VALUES ('"+ namee + "', '" + usnn + "', " + agee + ", " + yobb + ", " + markss + ");";
PreparedStatement preparedStatement = connection.prepareStatement(queryInsert);
preparedStatement.executeUpdate();
} else if (choice == 2) {
// Retrieving student details
String query = "SELECT * FROM students";
PreparedStatement preparedStatement = connection.prepareStatement(query);
ResultSet resultSet = preparedStatement.executeQuery();
// Displaying student details
while (resultSet.next()) {
String name = resultSet.getString("Name");
String usn = resultSet.getString("USN");
int age = resultSet.getInt("Age");
int yob = resultSet.getInt("YOB");
int marks = resultSet.getInt("Marks");
System.out.println("Name: " + name + ", USN: " +
usn + ", Age: " + age + ", YearofBirth: " + yob
+ ", Marks: " + marks);
}
} else if (choice == 3) {
System.out.println("Please enter the USN of the student to be deleted: ");
String usndel = sc.nextLine();
String queryDel = "DELETE FROM students WHERE USN='" + usndel + "'";
PreparedStatement preparedStatement = connection.prepareStatement(queryDel);
preparedStatement.executeUpdate();
} else if (choice == 4) {
System.out.println("Please enter the USN of the student to be updated: ");
String usnUpd = sc.nextLine();
System.out.println("Please enter the new name: ");
String newName = sc.nextLine();
String updQuery = "UPDATE students SET name=?WHERE USN=?";
PreparedStatement pstmt = connection.prepareStatement(updQuery);
pstmt.setString(1, newName);
pstmt.setString(2, usnUpd);
pstmt.executeUpdate();
} else if (choice == 5) {
System.out.println("Please enter the table's name to be joined to the current table: ");
String tableName = sc.nextLine();
String joinQuery = "SELECT * FROM students NATURAL JOIN " + tableName;
PreparedStatement pstmt = connection.prepareStatement(joinQuery);
ResultSet rs = pstmt.executeQuery();
ResultSetMetaData rsmd = rs.getMetaData();
int columnCount = rsmd.getColumnCount();
while (rs.next()) {
for (int i = 1; i <= columnCount; i++) {
System.out.print(rs.getString(i) + "\t");
}
System.out.println();
}
} else if (choice == 6) {
System.out.println("The maximum marks has been scored by: ");
String maxQuery = "SELECT Name FROM students WHERE Marks = (SELECT MAX(Marks) FROM students)";
PreparedStatement pstmt = connection.prepareStatement(maxQuery);
ResultSet rs = pstmt.executeQuery();
ResultSetMetaData rsmd = rs.getMetaData();
int columnCount = rsmd.getColumnCount();
while (rs.next()) {
for (int i = 1; i <= columnCount; i++) {
System.out.print(rs.getString(i) + "\t");
}
System.out.println();
}
} else if (choice == 7) {
String viewQuery = "CREATE VIEW Mark1 AS SELECT Name,Marks FROM students WHERE Marks > 90";
PreparedStatement pstmt = connection.prepareStatement(viewQuery);
pstmt.executeUpdate();
String selectQuery = "SELECT * FROM Mark1";
pstmt = connection.prepareStatement(selectQuery);
ResultSet rs = pstmt.executeQuery();
ResultSetMetaData rsmd = rs.getMetaData();
int columnCount = rsmd.getColumnCount();
while (rs.next()) {
for (int i = 1; i <= columnCount; i++) {
System.out.print(rs.getString(i) + "\t");
}
System.out.println();
}
}
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
// import java.sql.*;
// public class prog {
// // JDBC URL, username, and password of MySQL server
// private static final String JDBC_URL = "jdbc:mysql://localhost:3306/students";
// private static final String DB_USER = "root";
// private static final String DB_PASSWORD = "";
// public static void main(String[] args) {
// // Step 1: Establishing a Connection
// try (Connection connection = DriverManager.getConnection(JDBC_URL, DB_USER, DB_PASSWORD)) {
// // Step 2: Creating a Statement
// Statement statement = connection.createStatement();
// // Step 3: CRUD Operations
// // CREATE operation
// String createQuery = "INSERT INTO stud (id, name, age, department) VALUES (1, 'John', 21, 'Computer Science')";
// statement.executeUpdate(createQuery);
// System.out.println("Record created successfully.");
// // READ operation
// String readQuery = "SELECT * FROM stud";
// ResultSet resultSet = statement.executeQuery(readQuery);
// while (resultSet.next()) {
// int id = resultSet.getInt("id");
// String name = resultSet.getString("name");
// int age = resultSet.getInt("age");
// String department = resultSet.getString("department");
// System.out.println("ID: " + id + ", Name: " + name + ", Age: " + age + ", Department: " + department);
// }
// // UPDATE operation
// String updateQuery = "UPDATE stud SET age = 22 WHERE name = 'John'";
// statement.executeUpdate(updateQuery);
// System.out.println("Record updated successfully.");
// // DELETE operation
// String deleteQuery = "DELETE FROM stud WHERE name = 'John'";
// statement.executeUpdate(deleteQuery);
// System.out.println("Record deleted successfully.");
// } catch (SQLException e) {
// e.printStackTrace();
// }
// }
// }