Migrate to use JDBC;
This commit is contained in:
parent
b899d174a4
commit
96a5d83c5e
6
pom.xml
6
pom.xml
|
@ -76,12 +76,6 @@
|
|||
<version>0.1-SNAPSHOT</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
<version>8.0.30</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.xerial</groupId>
|
||||
<artifactId>sqlite-jdbc</artifactId>
|
||||
|
|
|
@ -1,35 +1,45 @@
|
|||
package com.pretzel.dev.villagertradelimiter.database;
|
||||
|
||||
import com.mysql.cj.jdbc.MysqlConnectionPoolDataSource;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import com.pretzel.dev.villagertradelimiter.VillagerTradeLimiter;
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
|
||||
public class MySQL extends Database {
|
||||
private final MysqlConnectionPoolDataSource source;
|
||||
private String hostname;
|
||||
private int port;
|
||||
private String database;
|
||||
private String username;
|
||||
private String password;
|
||||
private String encoding;
|
||||
private boolean useSsl;
|
||||
|
||||
public MySQL(final JavaPlugin instance, final ConfigurationSection cfg) {
|
||||
public MySQL(final VillagerTradeLimiter instance, final ConfigurationSection cfg) {
|
||||
super(instance);
|
||||
this.source = new MysqlConnectionPoolDataSource();
|
||||
this.load(cfg);
|
||||
}
|
||||
|
||||
public void load(final ConfigurationSection cfg) {
|
||||
this.source.setServerName(cfg.getString("host", "localhost"));
|
||||
this.source.setPort(cfg.getInt("port", 3306));
|
||||
this.source.setDatabaseName(cfg.getString("database", "sagas_holo"));
|
||||
this.source.setUser(cfg.getString("username", "root"));
|
||||
this.source.setPassword(cfg.getString("password", "root"));
|
||||
try {
|
||||
this.source.setCharacterEncoding(cfg.getString("encoding", "utf8"));
|
||||
this.source.setUseSSL(cfg.getBoolean("useSSL", false));
|
||||
} catch (SQLException ignored) {}
|
||||
|
||||
this.hostname = cfg.getString("host", "localhost");
|
||||
this.port = cfg.getInt("port", 3306);
|
||||
this.database = cfg.getString("database", "sagas_holo");
|
||||
this.username = cfg.getString("username", "root");
|
||||
this.password = cfg.getString("password", "root");
|
||||
this.encoding = cfg.getString("encoding", "utf8");
|
||||
this.useSsl = cfg.getBoolean("useSSL", false);
|
||||
this.test();
|
||||
}
|
||||
|
||||
public boolean isMySQL() { return true; }
|
||||
public DataSource getSource() { return this.source; }
|
||||
|
||||
@Override
|
||||
public Connection getConnection() throws SQLException {
|
||||
return DriverManager.getConnection(
|
||||
"jdbc:mysql://" + hostname + ":" + port + "/" + database
|
||||
+ "?autoReconnect=true&useSSL=" + useSsl + "&characterEncoding=" + encoding,
|
||||
username,
|
||||
password
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,25 +1,31 @@
|
|||
package com.pretzel.dev.villagertradelimiter.database;
|
||||
|
||||
import com.pretzel.dev.villagertradelimiter.VillagerTradeLimiter;
|
||||
import java.nio.file.Path;
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import org.sqlite.javax.SQLiteConnectionPoolDataSource;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
public class SQLite extends Database {
|
||||
private final SQLiteConnectionPoolDataSource source;
|
||||
|
||||
public SQLite(final JavaPlugin instance) {
|
||||
private Path path;
|
||||
|
||||
public SQLite(final VillagerTradeLimiter instance) {
|
||||
super(instance);
|
||||
this.source = new SQLiteConnectionPoolDataSource();
|
||||
this.load(null);
|
||||
}
|
||||
|
||||
public void load(final ConfigurationSection cfg) {
|
||||
this.source.setUrl("jdbc:sqlite:"+instance.getDataFolder().getPath()+"/database.db");
|
||||
path = instance.getDataFolder().toPath().resolve("database.db");
|
||||
this.test();
|
||||
}
|
||||
|
||||
public boolean isMySQL() { return false; }
|
||||
public DataSource getSource() { return this.source; }
|
||||
|
||||
@Override
|
||||
protected Connection getConnection() throws SQLException {
|
||||
return DriverManager.getConnection("jdbc:sqlite:" + path);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue