-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathMySQL.java
More file actions
73 lines (57 loc) · 2.99 KB
/
Copy pathMySQL.java
File metadata and controls
73 lines (57 loc) · 2.99 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
package com.code.advancedsql;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class MySQL extends SQL {
/**
* Create MySQL connection.
* Do not worry about creating the database, AdvancedSQL takes care of it for you.
* @param host MySQL server host.
* @param port MySQL server port.
* @param username MySQL server username.
* @param password MySQL server password.
* @param database MySQL server database. If not exists AdvancedSQL creates it for you.
* @param attributes MySQL server connection attributes.
* @throws SQLException Exception when something goes wrong.
*/
public MySQL(String host, int port, String username, String password, String database, String attributes) throws SQLException {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
this.connect(host, port, username, password, database, attributes);
} catch (SQLException e) {
if (e.getErrorCode() == 1049) {
createDatabase(host, port, username, password, database, attributes);
connect(host, port, username, password, database, attributes);
return;
}
throw new SQLException(e.getMessage(), e);
} catch (ClassNotFoundException e) {
this.connected = false;
throw new SQLException("Can't load JDBC Driver", e);
}
}
/**
* Create MySQL connection.
* Do not worry about creating the database, AdvancedSQL takes care of it for you.
* @param host MySQL server host.
* @param port MySQL server port.
* @param username MySQL server username.
* @param password MySQL server password.
* @param database MySQL server database. If not exists AdvancedSQL creates it for you.
* @throws SQLException Exception when something goes wrong.
*/
public MySQL(String host, int port, String username, String password, String database) throws SQLException {
this(host, port, username, password, database, "useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC&autoReconnect=true");
}
private void connect(String host, int port, String username, String password, String database, String attributes) throws SQLException {
connection = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database + (attributes.isEmpty() ? "" : "?" + attributes), username, password);
this.connected = true;
}
private void createDatabase(String host, int port, String username, String password, String database, String attributes) throws SQLException {
connection = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + (attributes.isEmpty() ? "" : "?" + attributes), username, password);
Statement statement = connection.createStatement();
statement.executeUpdate("CREATE DATABASE " + database);
connection.close();
connection = null;
}
}