版本更新至:3.8

调整:MySQLConnection 工具重构
This commit is contained in:
坏黑 2018-03-14 20:27:03 +08:00
parent 0179e269b5
commit 325ffe8906
2 changed files with 381 additions and 143 deletions

View File

@ -6,7 +6,7 @@ website: http://www.15imc.com/index.html
main: me.skymc.taboolib.Main main: me.skymc.taboolib.Main
version: 3.79 version: 3.8
commands: commands:
taboolib: taboolib:

View File

@ -1,6 +1,5 @@
package me.skymc.taboolib.mysql.protect; package me.skymc.taboolib.mysql.protect;
import java.io.File;
import java.sql.Connection; import java.sql.Connection;
import java.sql.DriverManager; import java.sql.DriverManager;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
@ -13,23 +12,36 @@ import java.util.List;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import lombok.Getter;
import lombok.Setter;
import me.skymc.taboolib.Main; import me.skymc.taboolib.Main;
public class MySQLConnection { public class MySQLConnection {
@Getter
private String url; private String url;
@Getter
private String user; private String user;
@Getter
private String port; private String port;
@Getter
private String password; private String password;
@Getter
private String database; private String database;
@Getter
private String connectionUrl; private String connectionUrl;
private Connection connection = null; @Getter
private Connection connection;
@Getter
@Setter
private Plugin plugin;
@Getter
@Setter
private boolean fallReconnection = true;
private int recheck = 10; private int recheck = 10;
private Thread recheckThread; private Thread recheckThread;
private Plugin plugin;
public MySQLConnection(String url, String user, String port, String password, String database) { public MySQLConnection(String url, String user, String port, String password, String database) {
this(url, user, port, password, database, 10, Main.getInst()); this(url, user, port, password, database, 10, Main.getInst());
} }
@ -87,14 +99,6 @@ public class MySQLConnection {
} }
} }
public Plugin getPlugin() {
return this.plugin;
}
public void setPlugin(Plugin plugin) {
this.plugin = plugin;
}
public void setReCheckSeconds(int s) { public void setReCheckSeconds(int s) {
this.recheck = s; this.recheck = s;
} }
@ -118,9 +122,13 @@ public class MySQLConnection {
public void closeConnection() { public void closeConnection() {
try { try {
connection.close(); connection.close();
} catch (Exception e) {
//
}
try {
recheckThread.stop(); recheckThread.stop();
} catch (SQLException e) { } catch (Exception e) {
// TODO Auto-generated catch block //
} }
} }
@ -147,23 +155,6 @@ public class MySQLConnection {
return execute("alter table `" + name + "` drop `" + column + "`"); return execute("alter table `" + name + "` drop `" + column + "`");
} }
public boolean deleteValue(String name, String column, Object columnValue) {
return execute("delete from `" + name + "` where `" + column + "` = " + parseToString(columnValue));
}
public boolean setValue(String name, String column, Object columnValue, String valueColumn, Object value) {
return setValue(name, column, columnValue, valueColumn, value, false);
}
public boolean setValue(String name, String column, Object columnValue, String valueColumn, Object value, boolean append) {
if (!append) {
return execute("update `" + name + "` set `" + valueColumn + "` = " + parseToString(value) + " where `" + column + "` = " + parseToString(columnValue));
}
else {
return execute("update `" + name + "` set `" + valueColumn + "` = " + valueColumn + " + " + parseToString(value) + " where `" + column + "` = " + parseToString(columnValue));
}
}
public void addColumn(String name, Column... columns) { public void addColumn(String name, Column... columns) {
for (Column column : columns) { for (Column column : columns) {
execute("alter table " + name + " add " + column.toString()); execute("alter table " + name + " add " + column.toString());
@ -188,14 +179,124 @@ public class MySQLConnection {
return execute("alter table " + name + " change `" + oldColumn + "` `" + newColumn.split("/")[0] + "` " + newColumn.split("/")[1]); return execute("alter table " + name + " change `" + oldColumn + "` `" + newColumn.split("/")[0] + "` " + newColumn.split("/")[1]);
} }
public boolean intoValue(String name, Object... values) { /**
StringBuilder sb = new StringBuilder(); * 删除数据
for (Object value : values) { *
sb.append(parseToString(value) + ", "); * @param name 名称
* @param column 参考列
* @param columnValue 参考值
* @return boolean
*/
public boolean deleteValue(String name, String column, Object columnValue) {
PreparedStatement pstmt = null;
ResultSet resultSet = null;
try {
pstmt = connection.prepareStatement("delete from `" + name + "` where `" + column + "` = ?");
pstmt.setObject(1, columnValue);
pstmt.executeUpdate();
} catch (Exception e) {
print("数据库命令执行出错");
print("错误原因: " + e.getMessage());
// 重新连接
if (fallReconnection && e.getMessage().contains("closed")) {
connect();
}
} finally {
freeResult(resultSet, pstmt);
} }
return execute("insert into " + name + " values(null, " + sb.substring(0, sb.length() - 2) + ")"); return false;
} }
/**
* 写入数据
*
* @param name 名称
* @param column 参考列
* @param columnValue 参考值
* @param valueColumn 数据列
* @param value 数据值
* @return boolean
*/
public boolean setValue(String name, String column, Object columnValue, String valueColumn, Object value) {
return setValue(name, column, columnValue, valueColumn, value, false);
}
/**
* 写入数据
*
* @param name 名称
* @param column 参考列
* @param columnValue 参考值
* @param valueColumn 数据列
* @param value 数据值
* @param append 是否追加数据列类型必须为数字
* @return boolean
*/
public boolean setValue(String name, String column, Object columnValue, String valueColumn, Object value, boolean append) {
PreparedStatement pstmt = null;
ResultSet resultSet = null;
try {
if (append) {
pstmt = connection.prepareStatement("update `" + name + "` set `" + valueColumn + "` = `" + valueColumn + "` + ? where `" + column + "` = ?");
} else {
pstmt = connection.prepareStatement("update `" + name + "` set `" + valueColumn + "` = ? where `" + column + "` = ?");
}
pstmt.setObject(1, value);
pstmt.setObject(2, columnValue);
pstmt.executeUpdate();
} catch (Exception e) {
print("数据库命令执行出错");
print("错误原因: " + e.getMessage());
// 重新连接
if (fallReconnection && e.getMessage().contains("closed")) {
connect();
}
} finally {
freeResult(resultSet, pstmt);
}
return false;
}
/**
* 插入数据
*
* @param name 名称
* @param values
* @return boolean
*/
public boolean intoValue(String name, Object... values) {
StringBuilder sb = new StringBuilder();
for (int i = 0 ; i < values.length ; i++) {
sb.append("?, ");
}
PreparedStatement pstmt = null;
ResultSet resultSet = null;
try {
pstmt = connection.prepareStatement("insert into `" + name + "` values(null, " + sb.substring(0, sb.length() - 2) + ")");
for (int i = 0 ; i < values.length ; i++) {
pstmt.setObject(i + 1, values[i]);
}
pstmt.executeUpdate();
} catch (Exception e) {
print("数据库命令执行出错");
print("错误原因: " + e.getMessage());
// 重新连接
if (fallReconnection && e.getMessage().contains("closed")) {
connect();
}
} finally {
freeResult(resultSet, pstmt);
}
return false;
}
/**
* 创建数据表
*
* @param name 名称
* @param columns 列表
* @return boolean
*/
public boolean createTable(String name, Column... columns) { public boolean createTable(String name, Column... columns) {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
for (Column column : columns) { for (Column column : columns) {
@ -204,209 +305,350 @@ public class MySQLConnection {
return execute("create table if not exists " + name + " (id int(1) not null primary key auto_increment, " + sb.substring(0, sb.length() - 2) + ")"); return execute("create table if not exists " + name + " (id int(1) not null primary key auto_increment, " + sb.substring(0, sb.length() - 2) + ")");
} }
/**
* 创建数据表
*
* @param name 名称
* @param columns 列表
* @return boolean
*/
public boolean createTable(String name, String... columns) { public boolean createTable(String name, String... columns) {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
for (String column : columns) { for (String column : columns) {
if (!column.contains("/")) { if (!column.contains("/")) {
sb.append("`" + column + "` text, "); sb.append("`" + column + "` text, ");
} } else {
else {
sb.append("`" + column.split("/")[0] + "` " + column.split("/")[1] + ", "); sb.append("`" + column.split("/")[0] + "` " + column.split("/")[1] + ", ");
} }
} }
return execute("create table if not exists " + name + " (id int(1) not null primary key auto_increment, " + sb.substring(0, sb.length() - 2) + ")"); return execute("create table if not exists " + name + " (id int(1) not null primary key auto_increment, " + sb.substring(0, sb.length() - 2) + ")");
} }
/**
* 检查数据表是否存在
*
* @param name 名称
* @return boolean
*/
public boolean isExists(String name) { public boolean isExists(String name) {
PreparedStatement pstmt = null;
ResultSet resultSet = null;
try { try {
PreparedStatement pstmt = connection.prepareStatement("select table_name FROM information_schema.TABLES where table_name = " + parseToString(name)); pstmt = connection.prepareStatement("select table_name FROM information_schema.TABLES where table_name = ?");
ResultSet resultSet = pstmt.executeQuery(); pstmt.setString(1, name);
resultSet = pstmt.executeQuery();
while (resultSet.next()) { while (resultSet.next()) {
return true; return true;
} }
resultSet.close(); } catch (Exception e) {
pstmt.close();
}
catch (Exception e) {
print("数据库命令执行出错"); print("数据库命令执行出错");
print("错误原因: " + e.getMessage()); print("错误原因: " + e.getMessage());
if (e.getMessage().contains("closed")) { // 重新连接
if (fallReconnection && e.getMessage().contains("closed")) {
connect(); connect();
} }
} finally {
freeResult(resultSet, pstmt);
} }
return false; return false;
} }
/**
* 检查数据是否存在
*
* @param name 名称
* @param column 列表名
* @param columnValue 列表值
* @return boolean
*/
public boolean isExists(String name, String column, Object columnValue) { public boolean isExists(String name, String column, Object columnValue) {
PreparedStatement pstmt = null;
ResultSet resultSet = null;
try { try {
PreparedStatement pstmt = connection.prepareStatement("select * from `" + name + "` where `" + column + "` = " + parseToString(columnValue)); pstmt = connection.prepareStatement("select * from `" + name + "` where `" + column + "` = ?");
ResultSet resultSet = pstmt.executeQuery(); pstmt.setObject(1, columnValue);
resultSet = pstmt.executeQuery();
while (resultSet.next()) { while (resultSet.next()) {
return true; return true;
} }
resultSet.close(); } catch (Exception e) {
pstmt.close();
}
catch (Exception e) {
print("数据库命令执行出错"); print("数据库命令执行出错");
print("错误原因: " + e.getMessage()); print("错误原因: " + e.getMessage());
if (e.getMessage().contains("closed")) { // 重新连接
if (fallReconnection && e.getMessage().contains("closed")) {
connect(); connect();
} }
} finally {
freeResult(resultSet, pstmt);
} }
return false; return false;
} }
/**
* 获取所有列表名称不含主键
*
* @param name 名称
* @return {@link List}
*/
public List<String> getColumns(String name) { public List<String> getColumns(String name) {
return getColumns(name, false); return getColumns(name, false);
} }
/**
* 获取所有列表名称
*
* @param name 名称
* @param primary 是否获取主键
* @return {@link List}
*/
public List<String> getColumns(String name, boolean primary) { public List<String> getColumns(String name, boolean primary) {
List<String> list = new ArrayList<>(); List<String> list = new ArrayList<>();
PreparedStatement pstmt = null;
ResultSet resultSet = null;
try { try {
PreparedStatement pstmt = connection.prepareStatement("select column_name from information_schema.COLUMNS where table_name = " + parseToString(name)); pstmt = connection.prepareStatement("select column_name from information_schema.COLUMNS where table_name = ?");
ResultSet resultSet = pstmt.executeQuery(); pstmt.setString(1, name);
resultSet = pstmt.executeQuery();
while (resultSet.next()) { while (resultSet.next()) {
list.add(resultSet.getString(1)); list.add(resultSet.getString(1));
} }
resultSet.close(); } catch (Exception e) {
pstmt.close();
}
catch (Exception e) {
print("数据库命令执行出错"); print("数据库命令执行出错");
print("错误原因: " + e.getMessage()); print("错误原因: " + e.getMessage());
if (e.getMessage().contains("closed")) { // 重新连接
if (fallReconnection && e.getMessage().contains("closed")) {
connect(); connect();
} }
} finally {
freeResult(resultSet, pstmt);
} }
// 是否获取主键
if (!primary) { if (!primary) {
list.remove("id"); list.remove("id");
} }
return list; return list;
} }
/**
* 获取单项数据
*
* @param name 名称
* @param column 参考列
* @param columnValue 参考值
* @param valueColumn 数据列
* @return Object
*/
public Object getValue(String name, String column, Object columnValue, String valueColumn) { public Object getValue(String name, String column, Object columnValue, String valueColumn) {
PreparedStatement pstmt = null;
ResultSet resultSet = null;
try { try {
PreparedStatement pstmt = connection.prepareStatement("select * from `" + name + "` where `" + column + "` = " + parseToString(columnValue) + " limit 1"); pstmt = connection.prepareStatement("select * from `" + name + "` where `" + column + "` = ? limit 1");
ResultSet resultSet = pstmt.executeQuery(); pstmt.setObject(1, columnValue);
resultSet = pstmt.executeQuery();
while (resultSet.next()) { while (resultSet.next()) {
return resultSet.getObject(valueColumn); return resultSet.getObject(valueColumn);
} }
resultSet.close(); } catch (Exception e) {
pstmt.close();
}
catch (Exception e) {
print("数据库命令执行出错"); print("数据库命令执行出错");
print("错误原因: " + e.getMessage()); print("错误原因: " + e.getMessage());
if (e.getMessage().contains("closed")) { // 重新连接
if (fallReconnection && e.getMessage().contains("closed")) {
connect(); connect();
} }
} finally {
freeResult(resultSet, pstmt);
} }
return null; return null;
} }
/**
* 获取单项数据根据主键倒叙排列后的最后一项
*
* @param name 名称
* @param column 参考列
* @param columnValue 参考值
* @param valueColumn 数据列
* @return Object
*/
public Object getValueLast(String name, String column, Object columnValue, String valueColumn) { public Object getValueLast(String name, String column, Object columnValue, String valueColumn) {
PreparedStatement pstmt = null;
ResultSet resultSet = null;
try { try {
PreparedStatement pstmt = connection.prepareStatement("select * from `" + name + "` where `" + column + "` = " + parseToString(columnValue) + " order by id desc limit 1"); pstmt = connection.prepareStatement("select * from `" + name + "` where `" + column + "` = ? order by id desc limit 1");
ResultSet resultSet = pstmt.executeQuery(); pstmt.setObject(1, columnValue);
resultSet = pstmt.executeQuery();
while (resultSet.next()) { while (resultSet.next()) {
return resultSet.getObject(valueColumn); return resultSet.getObject(valueColumn);
} }
resultSet.close(); } catch (Exception e) {
pstmt.close();
}
catch (Exception e) {
print("数据库命令执行出错"); print("数据库命令执行出错");
print("错误原因: " + e.getMessage()); print("错误原因: " + e.getMessage());
if (e.getMessage().contains("closed")) { // 重新连接
if (fallReconnection && e.getMessage().contains("closed")) {
connect(); connect();
} }
} finally {
freeResult(resultSet, pstmt);
} }
return null; return null;
} }
/**
* 获取多项数据根据主键倒叙排列后的最后一项
*
* @param name 名称
* @param column 参考列
* @param columnValue 参考值
* @param valueColumn 数据列
* @return {@link HashMap}
*/
public HashMap<String, Object> getValueLast(String name, String column, Object columnValue, String... valueColumn) { public HashMap<String, Object> getValueLast(String name, String column, Object columnValue, String... valueColumn) {
HashMap<String, Object> map = new HashMap<>(); HashMap<String, Object> map = new HashMap<>();
PreparedStatement pstmt = null;
ResultSet resultSet = null;
try { try {
PreparedStatement pstmt = connection.prepareStatement("select * from `" + name + "` where `" + column + "` = " + parseToString(columnValue) + " order by id desc limit 1"); pstmt = connection.prepareStatement("select * from `" + name + "` where `" + column + "` = ? order by id desc limit 1");
ResultSet resultSet = pstmt.executeQuery(); pstmt.setObject(1, columnValue);
resultSet = pstmt.executeQuery();
while (resultSet.next()) { while (resultSet.next()) {
for (String _column : valueColumn) { for (String _column : valueColumn) {
map.put(_column, resultSet.getObject(_column)); map.put(_column, resultSet.getObject(_column));
} }
break; break;
} }
resultSet.close(); } catch (Exception e) {
pstmt.close();
}
catch (Exception e) {
print("数据库命令执行出错"); print("数据库命令执行出错");
print("错误原因: " + e.getMessage()); print("错误原因: " + e.getMessage());
if (e.getMessage().contains("closed")) { // 重新连接
if (fallReconnection && e.getMessage().contains("closed")) {
connect(); connect();
} }
} finally {
freeResult(resultSet, pstmt);
} }
return map; return map;
} }
/**
* 获取多项数据单项多列
*
* @param name 名称
* @param column 参考列
* @param columnValue 参考值
* @param valueColumn 数据列
* @return {@link HashMap}
*/
public HashMap<String, Object> getValue(String name, String column, Object columnValue, String... valueColumn) { public HashMap<String, Object> getValue(String name, String column, Object columnValue, String... valueColumn) {
HashMap<String, Object> map = new HashMap<>(); HashMap<String, Object> map = new HashMap<>();
PreparedStatement pstmt = null;
ResultSet resultSet = null;
try { try {
PreparedStatement pstmt = connection.prepareStatement("select * from `" + name + "` where `" + column + "` = " + parseToString(columnValue)); pstmt = connection.prepareStatement("select * from `" + name + "` where `" + column + "` = ? limit 1");
ResultSet resultSet = pstmt.executeQuery(); pstmt.setObject(1, columnValue);
resultSet = pstmt.executeQuery();
while (resultSet.next()) { while (resultSet.next()) {
for (String _column : valueColumn) { for (String _column : valueColumn) {
map.put(_column, resultSet.getObject(_column)); map.put(_column, resultSet.getObject(_column));
} }
break; break;
} }
resultSet.close(); } catch (Exception e) {
pstmt.close();
}
catch (Exception e) {
print("数据库命令执行出错"); print("数据库命令执行出错");
print("错误原因: " + e.getMessage()); print("错误原因: " + e.getMessage());
if (e.getMessage().contains("closed")) { // 重新连接
if (fallReconnection && e.getMessage().contains("closed")) {
connect(); connect();
} }
} finally {
freeResult(resultSet, pstmt);
} }
return map; return map;
} }
/**
* 获取多项数据单列多列
*
* @param name 名称
* @param column 参考列
* @param size 获取数量-1 为无限制
* @return {@link List}
*/
public List<Object> getValues(String name, String column, int size) { public List<Object> getValues(String name, String column, int size) {
return getValues(name, column, size, false); return getValues(name, column, size, false);
} }
/**
* 获取多项数据单列多列
*
* @param name 名称
* @param column 参考列
* @param size 获取数量-1 位无限制
* @param desc 是否倒序
* @return {@link List}
*/
public List<Object> getValues(String name, String column, int size, boolean desc) { public List<Object> getValues(String name, String column, int size, boolean desc) {
List<Object> list = new ArrayList<>(); List<Object> list = new LinkedList<>();
PreparedStatement pstmt = null;
ResultSet resultSet = null;
try { try {
PreparedStatement pstmt = connection.prepareStatement("select * from `" + name + "` order by `" + column + (size == -1 ? "`" + (desc ? " desc" : "") : (desc ? "desc " : "") + "` limit " + size)); if (desc) {
ResultSet resultSet = pstmt.executeQuery(); pstmt = connection.prepareStatement("select * from `" + name + "` order by ? desc " + (size < 0 ? "" : " limit " + size));
} else {
pstmt = connection.prepareStatement("select * from `" + name + "` order by ? " + (size < 0 ? "" : " limit " + size));
}
pstmt.setString(1, column);
resultSet = pstmt.executeQuery();
while (resultSet.next()) { while (resultSet.next()) {
list.add(resultSet.getObject(column)); list.add(resultSet.getObject(column));
} }
resultSet.close(); } catch (Exception e) {
pstmt.close();
}
catch (Exception e) {
print("数据库命令执行出错"); print("数据库命令执行出错");
print("错误原因: " + e.getMessage()); print("错误原因: " + e.getMessage());
if (e.getMessage().contains("closed")) { // 重新连接
if (fallReconnection && e.getMessage().contains("closed")) {
connect(); connect();
} }
} finally {
freeResult(resultSet, pstmt);
} }
return list; return list;
} }
/**
* 获取多线数据多项多列
*
* @param name 名称
* @param sortColumn 参考列该列类型必须为数字
* @param size 获取数量-1 为无限制
* @param valueColumn 获取数据列
* @return {@link LinkedList}
*/
public LinkedList<HashMap<String, Object>> getValues(String name, String sortColumn, int size, String... valueColumn) { public LinkedList<HashMap<String, Object>> getValues(String name, String sortColumn, int size, String... valueColumn) {
return getValues(name, sortColumn, size, false, valueColumn); return getValues(name, sortColumn, size, false, valueColumn);
} }
/**
* 获取多项数据多项多列
*
* @param name 名称
* @param sortColumn 参考列该列类型必须为数字
* @param size 获取数量-1 为无限制
* @param desc 是否倒序
* @param valueColumn 获取数据列
* @return {@link LinkedList}
*/
public LinkedList<HashMap<String, Object>> getValues(String name, String sortColumn, int size, boolean desc, String... valueColumn) { public LinkedList<HashMap<String, Object>> getValues(String name, String sortColumn, int size, boolean desc, String... valueColumn) {
LinkedList<HashMap<String, Object>> list = new LinkedList<>(); LinkedList<HashMap<String, Object>> list = new LinkedList<>();
PreparedStatement pstmt = null;
ResultSet resultSet = null;
try { try {
PreparedStatement pstmt = connection.prepareStatement("select * from `" + name + "` order by `" + sortColumn + (size == -1 ? "`" + (desc ? " desc" : "") : (desc ? "desc " : "") + "` limit " + size)); if (desc) {
ResultSet resultSet = pstmt.executeQuery(); pstmt = connection.prepareStatement("select * from `" + name + "` order by ? desc " + (size < 0 ? "" : " limit " + size));
} else {
pstmt = connection.prepareStatement("select * from `" + name + "` order by ? " + (size < 0 ? "" : " limit " + size));
}
pstmt.setString(1, sortColumn);
resultSet = pstmt.executeQuery();
while (resultSet.next()) { while (resultSet.next()) {
HashMap<String, Object> map = new HashMap<>(); HashMap<String, Object> map = new HashMap<>();
for (String _column : valueColumn) { for (String _column : valueColumn) {
@ -414,35 +656,43 @@ public class MySQLConnection {
} }
list.add(map); list.add(map);
} }
resultSet.close(); } catch (Exception e) {
pstmt.close();
}
catch (Exception e) {
print("数据库命令执行出错"); print("数据库命令执行出错");
print("错误原因: " + e.getMessage()); print("错误原因: " + e.getMessage());
if (e.getMessage().contains("closed")) { // 重新连接
if (fallReconnection && e.getMessage().contains("closed")) {
connect(); connect();
} }
} finally {
freeResult(resultSet, pstmt);
} }
return list; return list;
} }
public boolean execute(String sql) { public boolean execute(String sql) {
PreparedStatement pstmt = null;
try { try {
// select * from user where userName = ? and password = ? pstmt = connection.prepareStatement(sql);
PreparedStatement pstmt = connection.prepareStatement(sql);
pstmt.execute(); pstmt.execute();
pstmt.close();
return true; return true;
} } catch (Exception e) {
catch (Exception e) {
print("数据库命令执行出错"); print("数据库命令执行出错");
print("错误原因: " + e.getMessage()); print("错误原因: " + e.getMessage());
print("错误命令: " + sql); print("错误命令: " + sql);
// 重连
if (e.getMessage().contains("closed")) { if (e.getMessage().contains("closed")) {
connect(); connect();
} }
return false; return false;
} finally {
try {
if (pstmt != null) {
pstmt.close();
}
}
catch (Exception e) {
//
}
} }
} }
@ -463,35 +713,33 @@ public class MySQLConnection {
} }
} }
public boolean connect_SQLite() {
try {
print("正在连接数据库");
print("地址: " + this.connectionUrl);
long time = System.currentTimeMillis();
connection = DriverManager.getConnection(connectionUrl);
print("数据库连接成功 (" + (System.currentTimeMillis() - time) + "ms)");
return true;
}
catch (SQLException e) {
print("数据库连接失败");
print("错误原因: " + e.getMessage());
print("错误代码: " + e.getErrorCode());
return false;
}
}
public Connection getConnection() {
return this.connection;
}
public String parseToString(Object object) {
return object instanceof String ? "'" + object + "'" : object.toString();
}
public void print(String message) { public void print(String message) {
System.out.println("[TabooLib - MySQL] " + message); System.out.println("[TabooLib - MySQL] " + message);
} }
/**
* 释放结果集
*
* @param resultSet 不知道叫什么
* @param pstmt 不知道叫什么
*/
private void freeResult(ResultSet resultSet, PreparedStatement pstmt) {
try {
if (resultSet != null) {
resultSet.close();
}
} catch (Exception e) {
//
}
try {
if (pstmt != null) {
pstmt.close();
}
} catch (Exception e) {
//
}
}
private boolean loadDriverMySQL() { private boolean loadDriverMySQL() {
try { try {
Class.forName("com.mysql.jdbc.Driver"); Class.forName("com.mysql.jdbc.Driver");
@ -502,16 +750,6 @@ public class MySQLConnection {
} }
} }
private boolean loadDriverSQLite() {
try {
Class.forName("org.sqlite.JDBC");
return true;
}
catch (ClassNotFoundException e) {
return false;
}
}
public static enum ColumnInteger { public static enum ColumnInteger {
TINYINT, SMALLINT, MEDIUMINT, INT, BIGINT; TINYINT, SMALLINT, MEDIUMINT, INT, BIGINT;