-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDBConnect.java
More file actions
65 lines (59 loc) · 1.86 KB
/
DBConnect.java
File metadata and controls
65 lines (59 loc) · 1.86 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
package PAT;
import java.sql.*;
/*
DB class
Externally sourced class that connects to the database and allows the execution of
queries and the retrieval of information.
*/
public class DBConnect {
private Connection connection;
private PreparedStatement statement;
public DBConnect() {
try {
// connect to database
connection = DriverManager.getConnection(
"jdbc:ucanaccess://Hostel.accdb");
System.out.println("Connection successful");
} catch (SQLException ex) {
System.out.println("Unable to connect");
}
}
/*
Method Update
Executes an update query
@parameters String update query
@return nothing
*/
public void update(String update) throws SQLException {
statement = connection.prepareStatement(update);
statement.executeUpdate();
statement.close();
}
/*
Method query
Executes a query and then returns the result of it
@parameters: String Select Query
@return: resultset of the result of the query
*/
public ResultSet query(String stmt) throws SQLException {
statement = connection.prepareStatement(stmt);
return statement.executeQuery();
}
public String processResultSet(ResultSet rs, String delimiter) {
String temp = "";
try {
ResultSetMetaData meta = rs.getMetaData();
int size = meta.getColumnCount();
while (rs.next()) {
for (int i = 1; i <= size; i++) {
Object value = rs.getObject(i);
temp += delimiter + value;
}
temp += "\n";
}
} catch (SQLException ex) {
ex.printStackTrace();
}
return temp;
}
}