-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathRetrieveAndStore.java
More file actions
125 lines (110 loc) · 3.3 KB
/
RetrieveAndStore.java
File metadata and controls
125 lines (110 loc) · 3.3 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
125
import java.sql.*;
/**
* Class to store and retrieve data from a database
* @author Sam
*/
public class RetrieveAndStore {
private static Connection conn;
private static Statement s;
/**
* Called from main to connect to the database
*/
public static void startDBConnection() {
try {
String database = "jdbc:ucanaccess://"+ System.getProperty("user.dir") + "/ProgramDB.mdb";
conn = DriverManager.getConnection(database, "", "gfg64%43df3*f\"A");
s = conn.createStatement();
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* Executes SQL queries to interact with file
* @param sqlString
* String of sql to be executed upon the database
*/
public static void sqlExecute(String sqlString) {
try {
s.execute(sqlString);
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* Executes SQL query to read all records from a table and return
* @param table
* table to read from with SQL
* @return ResultSet
* Returns set containing all records within the specified table
*/
public static ResultSet readAllRecords(String table) {
//Read all records from the database
ResultSet rs = null;
try {
rs = s.executeQuery("SELECT * FROM "+ table);
} catch (SQLException e) {
e.printStackTrace();
}
return rs;
}
/**
* Executes SQL query to find max ID in a table
* @param table
* table to read from with SQL
* @param columnName
* Column in the DB table to read max ID from
* @return maximum ID in the specified location as an integer
*/
public static int maxID (String table ,String columnName) {
int max = 0; //Stores the max ID
try {
ResultSet rs = s.executeQuery("SELECT " + columnName + " FROM " + table);
while (rs.next()) { //Loop through all IDs to find the max
if (rs.getInt(columnName) > max) {
max = rs.getInt(columnName);
}
}
} catch (SQLException e) {
System.out.println("Unable to take input from console");
e.printStackTrace();
}
return max;
}
/**
* Will update all ID's in a given tables column so it counts from 0 to the maximum row number
* CAUTION: will overwrite all data in the column
* @param tableName String, name of table to be edited
* @param columnName String, name of column to be edited
*/
public static void rowNumberUpdater(String tableName, String columnName){
ResultSet rs = RetrieveAndStore.readAllRecords(tableName);
int i = 1;
try {
while(rs.next()){ // Loop through all pools in table
RetrieveAndStore.sqlExecute(String.format("UPDATE %s SET %s = %d WHERE %s = %d;", tableName, columnName, i, columnName, rs.getInt(columnName)));
++i;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public static boolean exists(String tableName, String columnName,String name) throws SQLException {
ResultSet rs = RetrieveAndStore.readAllRecords(tableName);
int i = 0;
while (rs.next()){
if(name.equals(rs.getString(columnName))) return true;
}
return false;
}
/**
* Called from main to connect to the database
*/
public static void closeDBConnection () {
try {
s.close(); //Close the statement
conn.close(); //Close the database
} catch (SQLException e) {
e.printStackTrace();
}
}
}