-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathConnectionFactory.java
More file actions
59 lines (49 loc) · 1.8 KB
/
ConnectionFactory.java
File metadata and controls
59 lines (49 loc) · 1.8 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
import com.mysql.cj.jdbc.exceptions.SQLError;
import com.mysql.cj.jdbc.Driver;
import java.sql.*;
public class ConnectionFactory {
//
// public static void main(String[] args) {
// getConnection();
//
// }
//
// public static final String URL = "jdbc:mysql://localhost:3306/zcwdblab?serverTimezone=UTC";
// public static final String USER = "nick";
// public static final String PASS = "password";
//
// public static Connection getConnection() {
// try {
// DriverManager.registerDriver(new Driver());
// return DriverManager.getConnection(URL, USER, PASS);
// } catch (SQLException e) {
// throw new RuntimeException("ERROR : Can't connect to the database", e);
// }
// }
static void registerJDBCDriver() {
// Attempt to register JDBC Driver
try {
DriverManager.registerDriver(Driver.class.newInstance());
} catch (InstantiationException | IllegalAccessException | SQLException e1) {
throw new Error();
}
}
static Statement getScrollableStatement(Connection connection) {
int resultSetType = ResultSet.TYPE_SCROLL_INSENSITIVE;
int resultSetConcurrency = ResultSet.CONCUR_READ_ONLY;
try { // scrollable statements can be iterated more than once without closing
return connection.createStatement(resultSetType, resultSetConcurrency);
} catch (SQLException e) {
throw new Error(e);
}
}
static void executeStatement(Connection connection, String sqlStatement) {
try {
Statement statement = getScrollableStatement(connection);
statement.execute(sqlStatement);
connection.commit();
} catch (SQLException e) {
throw new Error(e);
}
}
}