forked from DenzelCode/AdvancedSQL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathISQL.java
More file actions
79 lines (62 loc) · 2.18 KB
/
Copy pathISQL.java
File metadata and controls
79 lines (62 loc) · 2.18 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
package com.code.advancedsql;
import com.code.advancedsql.query.IQuery;
import com.code.advancedsql.table.ITable;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public interface ISQL {
/**
* Prepare SQL query.
* @param query Query object that you want to prepare.
* @return A PreparedStatement object.
* @throws SQLException Exception when something goes wrong.
*/
PreparedStatement prepare(IQuery query) throws SQLException;
/**
* Get access to a table object to modify information.
* @param name Name of the table.
* @return A Table object which allows you to execute SQL query's.
*/
ITable table(String name);
/**
* Get connection state.
* @return true if connected, false if not.
*/
boolean isConnected();
/**
* @return Connection Object.
*/
Connection getConnection();
/**
* Convert a ResultSet into a list.
* @param resultSet Result set
* @return Returns a list of map with the column name and value.
* @throws SQLException Exception when something goes wrong.
*/
static List<Map<String, Object>> convertResultSetToList(ResultSet resultSet) throws SQLException {
ResultSetMetaData metaData = resultSet.getMetaData();
int columns = metaData.getColumnCount();
List<Map<String,Object>> list = new ArrayList<>();
while (resultSet.next()) {
Map<String,Object> row = new HashMap<>(columns);
for (int i = 1; i <= columns; i++) row.put(metaData.getColumnName(i), resultSet.getObject(i));
list.add(row);
}
resultSet.close();
return list;
}
/**
* Set statement parameters.
* @param statement Statement to modify.
* @param execute Parameters to assign.
* @throws SQLException Exception when something goes wrong.
*/
static void setStatementParameters(PreparedStatement statement, List<Object> execute) throws SQLException {
for (int i = 0; i < execute.size(); i++) {
Object value = execute.get(i);
statement.setObject(i + 1, value);
}
}
}