-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFaculty.java
More file actions
69 lines (59 loc) · 2.29 KB
/
Faculty.java
File metadata and controls
69 lines (59 loc) · 2.29 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
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Punya
*/
public class Faculty
{
private static Connection connection; //Variable for database connection
private static ArrayList<String> faculty = new ArrayList<String>(); //ArrayList to store faculty names
private static PreparedStatement addFaculty;
private static PreparedStatement getFacultyList;
private static ResultSet resultSet;
public static void addFaculty(String name)
{
connection = DBConnection.getConnection(); //Connecting to the database using the DBConnection class
try //Trying to connect to the database
{
addFaculty = connection.prepareStatement("insert into faculty (name) values (?)"); //SQL Query to insert faculty name into the faculty table
addFaculty.setString(1, name); //Setting the unknown value to the faculty name provided
addFaculty.executeUpdate(); //Executing the update to the table
}
catch(SQLException sqlException) //Throwing an error if there is a problem connecting to the database
{
sqlException.printStackTrace();
System.out.println("Could not open the database!");
System.exit(1);
}
}
public static ArrayList<String> getFacultyList()
{
connection = DBConnection.getConnection();
ArrayList<String> faculty = new ArrayList<String>();
try
{
getFacultyList = connection.prepareStatement("select name from faculty order by name"); //Getting all the names from the faculty list
resultSet = getFacultyList.executeQuery();
while(resultSet.next())
{
faculty.add(resultSet.getString(1));
}
}
catch(SQLException sqlException)
{
sqlException.printStackTrace();
System.out.println("Could not open the database!");
System.exit(1);
}
return faculty;
}
}