-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdmin.java
More file actions
113 lines (91 loc) · 3.38 KB
/
Admin.java
File metadata and controls
113 lines (91 loc) · 3.38 KB
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
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;
public class MySQLAccess {
private Connection connect = null;
private Statement statement = null;
private PreparedStatement preparedStatement = null;
private ResultSet resultSet = null;
public void readDataBase() throws Exception {
try {
Class.forName("com.mysql.jdbc.Driver"); //JDBC Driver for mySQL
connect = DriverManager
.getConnection("mysql -u root -p"
+ "user=sqluser&password=sqluserpw"); // connecting to the database
statement = connect.createStatement(); // Allows to connect sql query to database
resultSet = statement
.executeQuery("select * from student_details"); // Result of the sql query
writeResultSet(resultSet);
preparedStatement = connect
.prepareStatement("insert into student_details values (?, ?, ?, ? , ?)");
preparedStatement.setString(1, "rollno");
preparedStatement.setString(2, "name");
preparedStatement.setString(3, "branch");
preparedStatement.setInt(4, "phno");
preparedStatement.setInt(5, "parent_phno");
preparedStatement.executeUpdate();
preparedStatement = connect
.prepareStatement("SELECT rollno, name,branch from student_details");
resultSet = preparedStatement.executeQuery();
writeResultSet(resultSet);
preparedStatement = connect
.prepareStatement("delete from student_details where myuser= ? ; ");
preparedStatement.setString(1, "rollno");
preparedStatement.executeUpdate();
resultSet = statement
.executeQuery("select * from student_details");
writeMetaData(resultSet);
} catch (Exception e) {
throw e;
} finally {
close();
}
}
private void writeMetaData(ResultSet resultSet) throws SQLException {
// now get some metadata from the database
System.out.println("The columns in the table are: ");
System.out.println("Table: " + resultSet.getMetaData().getTableName(1));
for (int i = 1; i<= resultSet.getMetaData().getColumnCount(); i++){
System.out.println("Column " +i + " "+ resultSet.getMetaData().getColumnName(i));
}
}
private void writeResultSet(ResultSet resultSet) throws SQLException {
// resultSet is initialised before the first data set
while (resultSet.next()) {
// it is possible to get the columns via name
// also possible to get the columns via the column number
// which starts at 1
String roll = resultSet.getString("rollno");
String name = resultSet.getString("name");
String branch = resultSet.getString("branch");
String phn = resultSet.getInt("phno");
System.out.println("rollno: " + roll);
System.out.println("name: " + name);
System.out.println("branch: " + branch);
System.out.println("phone num: " +phno);
}
}
private void close() {
close(resultSet);
close(statement);
close(connect);
}
private void close(Closeable c) {
try {
if (c != null) {
c.close();
}
} catch (Exception e) {
}
}
}
public class Admin {
public static void main(String[] args) throws Exception {
MySQLAccess dao = new MySQLAccess();
dao.readDataBase();
}
}