-
Notifications
You must be signed in to change notification settings - Fork 0
/
StudentsQueries.java
181 lines (166 loc) · 4.89 KB
/
StudentsQueries.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
public class StudentsQueries {
private static final String DATABASE_URL="jdbc:derby:C:\\Users\\yehudit\\Documents\\מדעי המחשב - האוניברסיטה הפתוחה\\סדנה בתכנות מתקדם בשפת ג'אווה 20503\\JDBC Lecture\\students";
private static final String USERNAME="kerido";
private static final String PASSWORD="kerido";
private Connection connection;
private PreparedStatement selectAllStudents;
private PreparedStatement selectByCourse;;
private PreparedStatement insertNewStudent;
private PreparedStatement deleteByID;
public StudentsQueries()
{
try
{
connection=DriverManager.getConnection(DATABASE_URL, USERNAME, PASSWORD);
//create the first query
selectAllStudents=connection.prepareStatement("SELECT * FROM students");
//create second query
selectByCourse=connection.prepareStatement("SELECT firstName, lastName, numberID FROM students INNER JOIN studentsGrades ON students.studentID=studentsGrades.studentID WHERE numberID=?");
//create third query
insertNewStudent=connection.prepareStatement("INSERT INTO students "
+ "(studentID, firstName, lastName, address) "
+ "VALUES (?, ?, ?, ?)");
//create forth query
deleteByID=connection.prepareStatement("DELETE FROM students WHERE studentID=?");
}
catch (SQLException sqlException)
{
sqlException.printStackTrace();
System.exit(1);
}
}
//this method execute first query and presents her results
public void showAllStudents()
{
ResultSet resultSet=null;
try
{
resultSet=selectAllStudents.executeQuery();
ResultSetMetaData metaData=resultSet.getMetaData();
int numberOfColumns=metaData.getColumnCount();
System.out.println("Students table of Students Database:\n");
// display resultSet header
for (int i = 1; i <= numberOfColumns; i++)
System.out.printf("%-8s\t", metaData.getColumnName(i));
System.out.println();
// display each row
while (resultSet.next())
{
for (int i = 1; i <= numberOfColumns; i++)
System.out.printf("%8s\t", resultSet.getObject(i));
System.out.println();
}
System.out.println();
}
catch (SQLException sqlException)
{
sqlException.printStackTrace();
}
finally
{
try
{
resultSet.close();
}
catch (SQLException sqlException)
{
sqlException.printStackTrace();
close();
}
}
}
//this method execute second query and presents her results
public void showStudentsByCourse(int number)
{
ResultSet resultSet=null;
try
{
selectByCourse.setInt(1, number);
resultSet=selectByCourse.executeQuery();
ResultSetMetaData metaData=resultSet.getMetaData();
int numberOfColumns=metaData.getColumnCount();
System.out.println("Students which participants in course :" + number + "\n");
// display resultSet header
for (int i = 1; i <= numberOfColumns; i++)
System.out.printf("%-8s\t", metaData.getColumnName(i));
System.out.println();
// display each row
while (resultSet.next())
{
for (int i = 1; i <= numberOfColumns; i++)
System.out.printf("%8s\t", resultSet.getObject(i));
System.out.println();
}
System.out.println();
}
catch (SQLException sqlException)
{
sqlException.printStackTrace();
}
finally
{
try
{
resultSet.close();
}
catch (SQLException sqlException)
{
sqlException.printStackTrace();
close();
}
}
}
//this method execute third query and presents her results
public int addStudent(int ID, String privateName, String lastName, String address, int phone)
{
int result=0;
try
{
insertNewStudent.setInt(1, ID);
insertNewStudent.setString(2, privateName);
insertNewStudent.setString(3, lastName);
insertNewStudent.setString(4, address);
result=insertNewStudent.executeUpdate();
}
catch (SQLException sqlException)
{
sqlException.printStackTrace();
close();
}
return result;
}
//this method execute third query and presents her results
public int removeStudent (int ID)
{
int result=0;
try
{
deleteByID.setInt(1, ID);
result=deleteByID.executeUpdate();
}
catch (SQLException sqlException)
{
sqlException.printStackTrace();
close();
}
return result;
}
//close the database connection
public void close()
{
try
{
connection.close();
}
catch (SQLException sqlException)
{
sqlException.printStackTrace();
}
}
}