-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSQLTools.java
273 lines (195 loc) · 6.77 KB
/
SQLTools.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*
This is a helpful class with all needed SQL queries.
Including insert, remove, view, and getting all tables in database.
*/
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
public class SQLTools{
//url of database
private static String url = "";
//current working table
private static String currentDB = "inventory";
//Connect to db
public static Connection connect(){
Connection conn = null;
try {
conn = DriverManager.getConnection(url);
} catch (SQLException e) {
System.out.println(e.getMessage());
}
return conn;
}
//View table schema in print form
public static void viewTable(){
String sql = "SELECT * FROM " + currentDB;
try(Connection con = connect();){
Statement pstmt = con.createStatement();
ResultSet result = pstmt.executeQuery(sql);
System.out.println("item_name |" + " id |" + " status |" + " checked_in");
while(result.next()){
System.out.println(result.getString("item_name")+ "|" +
result.getString("id") + "|" +
result.getString("status") + "|"+
result.getString("checked_in"));
}
}catch(SQLException e){}
}
//Get list of all tables
public static ArrayList<String> getTables(){
ArrayList<String> tables = new ArrayList<String>();
try (Connection conn = connect();) {
ResultSet rs = conn.getMetaData().getTables(null, null, null, null);
while (rs.next()) {
if(rs.getString("TABLE_NAME").equals("sqlite_schema")){
continue;
}
String table = rs.getString("TABLE_NAME");
System.out.println(table);
tables.add(table);
}
} catch (SQLException e) {
System.out.println(e.getMessage());
}
return tables;
}
//Simple function to check if a table exists
public static boolean tableExists(String tableName){
ArrayList<String> tables = getTables();
if(tables.contains(tableName)){
return true;
}
return false;
}
public static void createTable(String tableName){
String sql = "CREATE TABLE " + tableName +
"(item_name TEXT," +
"id TEXT," +
"status TEXT," +
"checked_in INTEGER)";
try(Connection con = connect();){
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.executeUpdate();
}catch(SQLException e){}
setCurrentDB(tableName);
}
public static void deleteTable(String tableName){
String sql = "DROP TABLE " + tableName + ";";
try(Connection con = connect();){
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.executeUpdate();
}catch(SQLException e){}
}
public static void insert(String itemName, String id, Item.STATUS status, boolean checkedIn){
String sql = "INSERT INTO " + currentDB +
" VALUES (?, ?, ?, ?);";
try (Connection conn = connect();) {
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, itemName);
pstmt.setString(2, id);
pstmt.setString(3, status.name());
pstmt.setInt(4, boolToInt(checkedIn));
pstmt.executeUpdate();
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
//Insert an empty row into database
public static void insertBlank(){
String sql = "INSERT INTO " + currentDB +
" VALUES (?, ?, ?, ?);";
try (Connection conn = connect();) {
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, "");
pstmt.setString(2, "");
pstmt.setString(3, Item.STATUS.WAREHOUSE.name());
pstmt.setInt(4, 0);
pstmt.executeUpdate();
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
//Update row in database
//No support for changing ID
public static void updateRow(int column, Object id, Object data){
String sql;
try (Connection con = connect();) {
sql = "UPDATE " + currentDB + " SET item_name = ? WHERE id = ?;";
PreparedStatement pstmt = con.prepareStatement(sql);
System.out.println("col===" + column);
switch(column){
case 0:
sql = "UPDATE " + currentDB + " SET item_name = ? WHERE id = ?;";
pstmt = con.prepareStatement(sql);
pstmt.setString(1,data.toString());
pstmt.setString(2,id.toString());
break;
case 2:
sql = "UPDATE " + currentDB + " SET status = ? WHERE id = ?;";
pstmt = con.prepareStatement(sql);
pstmt.setString(1,data.toString());
pstmt.setString(2,id.toString());
break;
case 3:
sql = "UPDATE " + currentDB + " SET checked_in = ? WHERE id = ?;";
pstmt = con.prepareStatement(sql);
pstmt.setInt(1,boolToInt((boolean)data));
pstmt.setString(2,id.toString());
break;
}
pstmt.executeUpdate();
viewTable();
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
public static void deleteRow(String itemName, String id){
String sql = "DELETE FROM " + currentDB + " WHERE item_name IS '" + itemName +"' AND id IS '" + id + "';";
System.out.println(sql);
try (Connection con = connect();) {
Statement stat = con.createStatement();
stat.executeUpdate(sql);
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
//returns all data from table
//Passes it as an array of combined strings
public static Object[] loadTable(String table){
setCurrentDB(table);
ArrayList<Object> loadedTableData = new ArrayList<Object>();
String sql = "SELECT * FROM " + currentDB;
try(Connection con = connect();){
Statement pstmt = con.createStatement();
ResultSet result = pstmt.executeQuery(sql);
while(result.next()){
String data = result.getString("item_name") + "," +
result.getString("id") + "," +
result.getString("status") + "," +
result.getString("checked_in");
System.out.println(data);
loadedTableData.add(data);
}
}catch(SQLException e){}
System.out.println("dddomne");
return loadedTableData.toArray();
}
//Converts boolean from JTable into int for SQLite
public static int boolToInt(boolean bool){
return bool ? 1 : 0;
}
public static String getCurrentDB(){
return currentDB;
}
public static void setCurrentDB(String db){
currentDB = db;
}
//sets working directory of current database file
public static void setDBURL(String path){
url = "jdbc:sqlite:" + path;
}
}