mirror of
				https://e.coding.net/circlecloud/YumCore.git
				synced 2025-11-03 23:06:02 +00:00 
			
		
		
		
	
							
								
								
									
										184
									
								
								src/main/java/pw/yumc/YumCore/sql/core/DataBaseCore.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										184
									
								
								src/main/java/pw/yumc/YumCore/sql/core/DataBaseCore.java
									
									
									
									
									
										Normal 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 2015年12月14日 下午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);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										176
									
								
								src/main/java/pw/yumc/YumCore/sql/core/KeyValue.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										176
									
								
								src/main/java/pw/yumc/YumCore/sql/core/KeyValue.java
									
									
									
									
									
										Normal 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 2015年12月14日 下午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);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										96
									
								
								src/main/java/pw/yumc/YumCore/sql/core/MySQLCore.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										96
									
								
								src/main/java/pw/yumc/YumCore/sql/core/MySQLCore.java
									
									
									
									
									
										Normal 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 2015年12月14日 下午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;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										126
									
								
								src/main/java/pw/yumc/YumCore/sql/core/SQLiteCore.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										126
									
								
								src/main/java/pw/yumc/YumCore/sql/core/SQLiteCore.java
									
									
									
									
									
										Normal 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 2015年7月14日 下午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;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Reference in New Issue
	
	Block a user