Skip to content

Commit

Permalink
添加MySQL连接创建的工厂方法
Browse files Browse the repository at this point in the history
  • Loading branch information
Gk0Wk committed Jun 26, 2021
1 parent 403f9ee commit 38bde59
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 2 deletions.
4 changes: 3 additions & 1 deletion Violet.iml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<orderEntry type="library" name="Maven: com.google.guava:guava:21.0" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: com.google.code.gson:gson:2.8.0" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.yaml:snakeyaml:1.23" level="project" />
<orderEntry type="library" name="Maven: org.jetbrains:annotations:20.1.0" level="project" />
<orderEntry type="library" name="Maven: org.jetbrains:annotations:21.0.1" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: com.mojang:authlib:1.5.21" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.apache.commons:commons-lang3:3.3.2" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.apache.logging.log4j:log4j-core:2.0-beta9" level="project" />
Expand All @@ -38,5 +38,7 @@
<orderEntry type="library" name="Maven: net.jodah:expiringmap:0.5.8" level="project" />
<orderEntry type="library" name="Maven: net.kyori:event-api:3.0.0" level="project" />
<orderEntry type="library" name="Maven: org.checkerframework:checker-qual:2.5.4" level="project" />
<orderEntry type="library" name="Maven: mysql:mysql-connector-java:8.0.25" level="project" />
<orderEntry type="library" name="Maven: com.google.protobuf:protobuf-java:3.11.4" level="project" />
</component>
</module>
7 changes: 6 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>io.github.gk0wk</groupId>
<artifactId>Violet</artifactId>
<version>1.0.5</version>
<version>1.0.6</version>
<packaging>jar</packaging>
<name>Violet</name>
<url>https://gk0wk.github.io/</url>
Expand Down Expand Up @@ -51,6 +51,11 @@
<artifactId>helper</artifactId>
<version>5.6.5</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.25</version>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package io.github.gk0wk.violet.database;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

public class MySQLConnectionBuilder {
private String host = "localhost";
private int port = 3306;
private String database = null;
private String username = null;
private String password = null;
private final Map<String, String> params = new HashMap<>();

public MySQLConnectionBuilder host(String host) {
this.host = host;
return this;
}

public MySQLConnectionBuilder port(int port) {
this.port = port;
return this;
}

public MySQLConnectionBuilder database(String database) {
this.database = database;
return this;
}

public MySQLConnectionBuilder username(String username) {
this.username = username;
return this;
}

public MySQLConnectionBuilder password(String password) {
this.password = password;
return this;
}

public MySQLConnectionBuilder setParam(String param, String value) {
this.params.put(param, value);
return this;
}

public String getParam(String param) {
return this.params.get(param);
}

public Connection build() {
try {
Class.forName("com.mysql.cj.jdbc.Driver"); // "com.mysql.jdbc.Driver" is deprecated over JDBC6.
if (!this.params.containsKey("serverTimezone")) {
this.params.put("serverTimezone", "Asia/Shanghai");
}

StringBuilder sb = new StringBuilder();
sb.append("jdbc:mysql://").append(this.host).append(':').append(this.port).append('/').append(this.database);
if (!this.params.isEmpty()) {
sb.append('?');
this.params.forEach((key, value) -> sb.append(key).append('=').append(value).append('&'));
sb.deleteCharAt(sb.length() - 1);
}

return DriverManager.getConnection(sb.toString(), this.username, this.password);
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
return null;
}
}
}

0 comments on commit 38bde59

Please sign in to comment.