-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStudentQueries.java
More file actions
124 lines (101 loc) · 4.04 KB
/
StudentQueries.java
File metadata and controls
124 lines (101 loc) · 4.04 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
114
115
116
117
118
119
120
121
122
123
124
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
/**
*
* @author Admin
*/
public class StudentQueries {
private static Connection connection;
//private static ArrayList<String> faculty = new ArrayList<String>();
private static PreparedStatement addStudent;
private static PreparedStatement getAllStudents;
private static ResultSet resultSet;
private static ResultSet resultSet2;
private static PreparedStatement getStudent;
private static PreparedStatement dropStudent;
public static void addStudent(StudentEntry myStudent)
{
connection = DBConnection.getConnection();
try
{
addStudent = connection.prepareStatement("insert into app.studententry (studentid, firstname, lastname) values (?, ?, ?)");
addStudent.setString(1, myStudent.getStudentID());
addStudent.setString(2, myStudent.getFirstName());
addStudent.setString(3, myStudent.getLastName());
addStudent.executeUpdate();
}
catch(SQLException sqlException)
{
sqlException.printStackTrace();
}
}
public static ArrayList<StudentEntry> getAllStudents()
{
connection = DBConnection.getConnection();
ArrayList<StudentEntry> students = new ArrayList<StudentEntry>();
try
{
getAllStudents = connection.prepareStatement("select studentid, firstname, lastname from app.studententry order by studentid");
resultSet = getAllStudents.executeQuery();
while(resultSet.next())
{
String id = resultSet.getString(1);
String first = resultSet.getString(2);
String last = resultSet.getString(3);
StudentEntry newStudent = new StudentEntry(id, first, last);
students.add(newStudent);
}
}
catch(SQLException sqlException)
{
sqlException.printStackTrace();
}
return students;
}
public static StudentEntry getStudent(String studentID)
{
connection = DBConnection.getConnection();
ArrayList<StudentEntry> students = new ArrayList<StudentEntry>();
try
{
String[] result = studentID.split(", ");
getStudent = connection.prepareStatement("select studentid, firstname, lastname from app.studententry where firstname = ? and lastname = ?");
getStudent.setString(1, result[1]);
getStudent.setString(2, result[0]);
resultSet2 = getStudent.executeQuery();
if(resultSet2.next()) {
String id = resultSet2.getString(1);
String first = resultSet2.getString(2);
String last = resultSet2.getString(3);
StudentEntry newStudent = new StudentEntry(id, first, last);
students.add(newStudent);
}
}
catch(SQLException sqlException)
{
sqlException.printStackTrace();
}
return students.get(0);
}
public static void dropStudent(String studentID)
{
connection = DBConnection.getConnection();
try
{
dropStudent = connection.prepareStatement("delete from app.studententry where studentid = ?");
dropStudent.setString(1, studentID);
dropStudent.executeUpdate();
}
catch(SQLException sqlException)
{
sqlException.printStackTrace();
}
}
}