feat: 添加数据库核心

Signed-off-by: 502647092 <admin@yumc.pw>
merge/1/MERGE
502647092 2016-09-05 11:08:21 +08:00
parent eae10d3527
commit 26a49e8fbe
4 changed files with 582 additions and 0 deletions

View File

@ -0,0 +1,184 @@
package pw.yumc.YumCore.sql.core;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import pw.yumc.YumCore.bukkit.Log;
/**
*
*
* @since 20151214 1:26:15
* @author
*/
public abstract class DataBaseCore {
/**
*
*
* @param tableName
*
* @param fields
*
* @param Conditions
* -
* @return
* @throws SQLException
* SQL
*/
public abstract boolean createTables(final String tableName, final KeyValue fields, final String Conditions) throws SQLException;
/**
* SQL
*
* @param sql
* SQL
* @return
* @throws SQLException
* SQL
*/
public boolean execute(final String sql) throws SQLException {
debug(sql);
final Statement st = getStatement();
final boolean result = st.execute(sql);
st.close();
return result;
}
/**
* SQL()
*
* @param sql
* SQL
* @param obj
*
* @return
* @throws SQLException
* SQL
*/
public boolean execute(final String sql, final Object... obj) throws SQLException {
debug(sql);
final PreparedStatement ps = prepareStatement(sql);
for (int i = 0; i < obj.length; i++) {
ps.setObject(i + 1, obj[i]);
}
final boolean result = ps.execute(sql);
ps.close();
return result;
}
/**
* @return
*/
public String getAUTO_INCREMENT() {
return "AUTO_INCREMENT";
}
/**
* .
*
* @return
*/
public abstract Connection getConnection();
/**
*
*
* @param sql
* SQL
* @return
* @throws SQLException
* SQL
*/
public ResultSet query(final String sql) throws SQLException {
debug(sql);
final Statement st = getStatement();
final ResultSet result = st.executeQuery(sql);
return result;
}
/**
*
*
* @param sql
* SQL
* @return
* @throws SQLException
* SQL
*/
public int update(final String sql) throws SQLException {
debug(sql);
final Statement st = getStatement();
final int result = st.executeUpdate(sql);
st.close();
return result;
}
/**
* ()
*
* @param sql
* SQL
* @param obj
*
* @return
* @throws SQLException
* SQL
*/
public int update(final String sql, final Object[] obj) throws SQLException {
debug(sql);
final PreparedStatement ps = prepareStatement(sql);
for (int i = 0; i < obj.length; i++) {
ps.setObject(i + 1, obj[i]);
}
final int result = ps.executeUpdate(sql);
ps.close();
return result;
}
/**
*
*
* @param warn
*
*/
public void warn(final String warn) {
Log.warning(warn);
}
/**
* SQL
*
* @param sql
* SQL
*/
private void debug(final String sql) {
Log.debug("[SQL] " + sql);
}
/**
*
*
* @return
* @throws SQLException
* SQL
*/
protected Statement getStatement() throws SQLException {
return getConnection().createStatement();
}
/**
* ()
*
* @param sql
* SQL
* @return
* @throws SQLException
* SQL
*/
protected PreparedStatement prepareStatement(final String sql) throws SQLException {
return getConnection().prepareStatement(sql);
}
}

View File

@ -0,0 +1,176 @@
package pw.yumc.YumCore.sql.core;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry;
/**
*
*
* @since 20151214 1:26:24
* @author
*/
public class KeyValue {
private final HashMap<Object, Object> keyvalues = new HashMap<Object, Object>();
/**
*
*/
public KeyValue() {
}
/**
*
*
* @param key
*
* @param value
*
*/
public KeyValue(final String key, final Object value) {
add(key, value);
}
/**
*
*
* @param key
*
* @param value
*
* @return {@link KeyValue}
*/
public KeyValue add(final String key, final Object value) {
this.keyvalues.put(key, value);
return this;
}
/**
*
*
* @return
*/
public String[] getKeys() {
return this.keyvalues.keySet().toArray(new String[0]);
}
/**
*
*
* @param key
*
* @return
*/
public String getString(final String key) {
final Object obj = this.keyvalues.get(key);
return obj == null ? "" : obj.toString();
}
/**
*
*
* @return
*/
public Object[] getValues() {
final List<Object> keys = new ArrayList<Object>();
for (final Entry<Object, Object> next : this.keyvalues.entrySet()) {
keys.add(next.getValue());
}
return keys.toArray(new Object[0]);
}
/**
*
*
* @return
*/
public boolean isEmpty() {
return this.keyvalues.isEmpty();
}
/**
* SQL
*
* @return SQL
*/
public String toCreateString() {
final StringBuilder sb = new StringBuilder();
for (final Entry<Object, Object> next : this.keyvalues.entrySet()) {
sb.append("`");
sb.append(next.getKey());
sb.append("` ");
sb.append(next.getValue());
sb.append(", ");
}
return sb.toString().substring(0, sb.length() - 2);
}
/**
* SQL
*
* @return SQL
*/
public String toInsertString() {
String ks = "";
String vs = "";
for (final Entry<Object, Object> next : this.keyvalues.entrySet()) {
ks += "`" + next.getKey() + "`, ";
vs += "'" + next.getValue() + "', ";
}
return "(" + ks.substring(0, ks.length() - 2) + ") VALUES (" + vs.substring(0, vs.length() - 2) + ")";
}
/**
* @return
*/
public String toKeys() {
final StringBuilder sb = new StringBuilder();
for (final Object next : this.keyvalues.keySet()) {
sb.append("`");
sb.append(next);
sb.append("`, ");
}
return sb.toString().substring(0, sb.length() - 2);
}
@Override
public String toString() {
return this.keyvalues.toString();
}
/**
* SQL
*
* @return SQL
*/
public String toUpdateString() {
final StringBuilder sb = new StringBuilder();
for (final Entry<Object, Object> next : this.keyvalues.entrySet()) {
sb.append("`");
sb.append(next.getKey());
sb.append("`='");
sb.append(next.getValue());
sb.append("' ,");
}
return sb.substring(0, sb.length() - 2);
}
/**
* SQL
*
* @return SQL
*/
public String toWhereString() {
final StringBuilder sb = new StringBuilder();
for (final Entry<Object, Object> next : this.keyvalues.entrySet()) {
sb.append("`");
sb.append(next.getKey());
sb.append("`='");
sb.append(next.getValue());
sb.append("' and ");
}
return sb.substring(0, sb.length() - 5);
}
}

View File

@ -0,0 +1,96 @@
package pw.yumc.YumCore.sql.core;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
import org.bukkit.configuration.ConfigurationSection;
/**
*
*
* @since 20151214 1:26:39
* @author
*/
public class MySQLCore extends DataBaseCore {
private static final String driverName = "com.mysql.jdbc.Driver";
private Connection connection;
private final Properties info;
private final String url;
/**
*
*
* @param cfg
*
*/
public MySQLCore(final ConfigurationSection cfg) {
this(cfg.getString("ip", "127.0.0.1"), cfg.getInt("port", 3306), cfg.getString("database", "minecraft"), cfg.getString("username", "root"), cfg.getString("password", ""));
}
/**
*
*
* @param host
*
* @param port
*
* @param dbname
*
* @param username
*
* @param password
*
*/
public MySQLCore(final String host, final int port, final String dbname, final String username, final String password) {
this.info = new Properties();
this.info.put("autoReconnect", "true");
this.info.put("user", username);
this.info.put("password", password);
this.info.put("useUnicode", "true");
this.info.put("characterEncoding", "utf8");
this.url = "jdbc:mysql://" + host + ":" + port + "/" + dbname;
try {
Class.forName(driverName).newInstance();
} catch (final Exception e) {
warn("数据库初始化失败 请检查驱动 " + driverName + " 是否存在!");
}
}
/**
*
*
* @param tableName
*
* @param fields
*
* @param Conditions
* -
* @return
* @throws SQLException
* SQL
*/
@Override
public boolean createTables(final String tableName, final KeyValue fields, final String Conditions) throws SQLException {
final String sql = "CREATE TABLE IF NOT EXISTS `%s` ( %s %s ) ENGINE = InnoDB DEFAULT CHARSET=UTF8";
return execute(String.format(sql, tableName, fields.toCreateString(), Conditions == null ? "" : ", " + Conditions));
}
@Override
public Connection getConnection() {
try {
if (this.connection != null && !this.connection.isClosed()) {
return this.connection;
}
this.connection = DriverManager.getConnection(this.url, this.info);
return this.connection;
} catch (final SQLException e) {
warn("数据库操作出错: " + e.getMessage());// 得到出错信息
warn("登录URL: " + this.url); // 发生错误时,将连接数据库信息打印出来
warn("登录账户: " + this.info.getProperty("user"));
warn("登录密码: " + this.info.getProperty("password"));
return null;
}
}
}

View File

@ -0,0 +1,126 @@
package pw.yumc.YumCore.sql.core;
import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.plugin.Plugin;
/**
*
*
* @since 2015714 3:25:06
* @author
*/
public class SQLiteCore extends DataBaseCore {
private static final String driverName = "org.sqlite.JDBC";
private Connection connection;
private final File dbFile;
/**
*
*
* @param dbFile
*
*/
public SQLiteCore(final File dbFile) {
this.dbFile = dbFile;
if (this.dbFile.exists()) {
// So we need a new connection
try {
this.dbFile.createNewFile();
} catch (final IOException e) {
warn("数据库文件 " + dbFile.getAbsolutePath() + " 创建失败!");
e.printStackTrace();
}
}
try {
Class.forName(driverName).newInstance();
} catch (final Exception e) {
warn("数据库初始化失败 请检查驱动 " + driverName + " 是否存在!");
e.printStackTrace();
}
}
/**
*
*
* @param plugin
*
* @param cfg
*
*/
public SQLiteCore(final Plugin plugin, final ConfigurationSection cfg) {
this(plugin, cfg.getString("database"));
}
/**
*
*
* @param plugin
*
* @param filename
*
*/
public SQLiteCore(final Plugin plugin, final String filename) {
this.dbFile = new File(plugin.getDataFolder(), filename + ".db");
if (this.dbFile.exists()) {
// So we need a new connection
try {
this.dbFile.createNewFile();
} catch (final IOException e) {
warn("数据库文件 " + this.dbFile.getAbsolutePath() + " 创建失败!");
e.printStackTrace();
}
}
try {
Class.forName(driverName).newInstance();
} catch (final Exception e) {
warn("数据库初始化失败 请检查驱动 " + driverName + " 是否存在!");
e.printStackTrace();
}
}
/**
*
*
* @param filepath
*
*/
public SQLiteCore(final String filepath) {
this(new File(filepath));
}
@Override
public boolean createTables(final String tableName, final KeyValue fields, final String Conditions) throws SQLException {
final String sql = "CREATE TABLE IF NOT EXISTS `%s` ( %s %s )";
return execute(String.format(sql, tableName, fields.toCreateString().replace("AUTO_INCREMENT", "AUTOINCREMENT"), Conditions == null ? "" : " , " + Conditions));
}
/**
* @return
*/
@Override
public String getAUTO_INCREMENT() {
return "AUTOINCREMENT";
}
@Override
public Connection getConnection() {
try {
if (this.connection != null && !this.connection.isClosed()) {
return this.connection;
}
this.connection = DriverManager.getConnection("jdbc:sqlite:" + this.dbFile);
return this.connection;
} catch (final SQLException e) {
warn("数据库操作出错: " + e.getMessage());// 得到出错信息
warn("数据库文件: " + this.dbFile.getAbsolutePath()); // 发生错误时,将连接数据库信息打印出来
return null;
}
}
}