/**
 * 
 */
package org.generator.db;

import java.sql.Connection;
import java.sql.Driver;
import java.sql.SQLException;
import java.text.MessageFormat;
import java.util.Properties;

import org.generator.config.ConfigurationHolder;
import org.generator.config.PropertyHolder;
import org.generator.type.DBType;
import org.generator.util.ObjectFactory;
import org.generator.util.StringUtility;

/**
 * 数据库连接工厂，单列
 * @author yangziran
 * @version 1.0 2014年9月11日
 */
public class ConnectionFactory {

    private static ConnectionFactory instance = new ConnectionFactory();

    public static ConnectionFactory getInstance() {
        return instance;
    }

    /**
     * 构造方法
     */
    private ConnectionFactory() {
        super();
    }

    /**
     * 获取数据库驱动类
     * @param config 数据库连接属性
     * @return Driver 数据库驱动
     * @author yangziran
     */
    private Driver getDriver(ConfigurationHolder config) {

        String driverClass = config.getDriverClass();
        Driver driver;

        try {
            Class<?> clazz = ObjectFactory.externalClassForName(driverClass);
            driver = (Driver) clazz.newInstance();
        } catch (Exception e) {
            throw new RuntimeException(MessageFormat.format("获取JDBC Driver错误", new Object[] {}), e);
        }

        return driver;
    }

    /**
     * 获取数据库连接
     * @param config 数据库连接属性
     * @return Connection 数据库连接
     * @throws SQLException
     * @author yangziran
     */
    public Connection getConnection(ConfigurationHolder config) throws SQLException {
        Driver driver = getDriver(config);

        Properties props = new Properties();
        // // 设置可以获取remarks信息
        // props.setProperty("remarks", "true");
        // // 设置可以获取tables remarks信息
        // props.setProperty("useInformationSchema", "true");

        if (StringUtility.isNotEmpty(config.getUserId())) {
            props.setProperty("user", config.getUserId());
        }

        if (StringUtility.isNotEmpty(config.getPassword())) {
            props.setProperty("password", config.getPassword());
        }

        Connection conn = driver.connect(config.getConnectionURL(), props);

        if (conn == null) {
            throw new SQLException(MessageFormat.format("无法连接到数据库（可能是驱动/URL错误）", new Object[] {}));
        }

        return conn;
    }

    /**
     * 获取数据库连接
     * @return
     * @throws SQLException
     * @author yangziran
     */
    public static Connection getConnection() throws SQLException {

        ConfigurationHolder config = new ConfigurationHolder();
        // 获取到数据库类型
        String db = DBType.valueOf(PropertyHolder.getJDBCProperty("DB")).getValue();
        config.setDriverClass(PropertyHolder.getJDBCProperty(db + ".driverClass"));
        config.setConnectionURL(PropertyHolder.getJDBCProperty(db + ".url"));
        config.setUserId(PropertyHolder.getJDBCProperty(db + ".username"));
        config.setPassword(PropertyHolder.getJDBCProperty(db + ".password"));

        Connection connection = ConnectionFactory.getInstance().getConnection(config);

        return connection;
    }
}
@@ -1,16 +1,15 @@
 /**%0D%0A *
- 
 %0D%0A */%0D%0Ap
@@ -40,158 +40,8 @@
 %0D%0A%0D%0A
-import java.sql.Connection;%0D%0Aimport java.sql.Driver;%0D%0Aimport java.sql.SQLException;%0D%0Aimport java.text.MessageFormat;%0D%0Aimport java.util.Properties;%0D%0A%0D%0A
 impo
@@ -252,16 +252,166 @@
 ity;%0D%0A%0D%0A
+import java.sql.Connection;%0D%0Aimport java.sql.Driver;%0D%0Aimport java.sql.SQLException;%0D%0Aimport java.text.MessageFormat;%0D%0Aimport java.util.Properties;%0D%0A%0D%0A
 /**%0D%0A * 
@@ -422,16 +422,20 @@
 %E6%8E%A5%E5%B7%A5%E5%8E%82%EF%BC%8C%E5%8D%95%E5%88%97%0D%0A
+ *%0D%0A
  * @auth
@@ -800,16 +800,24 @@
 %E6%95%B0%E6%8D%AE%E5%BA%93%E9%A9%B1%E5%8A%A8%E7%B1%BB%0D%0A
+     *%0D%0A
      * @
@@ -1315,17 +1315,16 @@
 Object%5B%5D
- 
 %7B%7D), e);
@@ -1388,32 +1388,40 @@
      * %E8%8E%B7%E5%8F%96%E6%95%B0%E6%8D%AE%E5%BA%93%E8%BF%9E%E6%8E%A5%0D%0A
+     *%0D%0A
      * @param co
@@ -1716,27 +1716,24 @@
 %0D%0A        //
- //
  %E8%AE%BE%E7%BD%AE%E5%8F%AF%E4%BB%A5%E8%8E%B7%E5%8F%96remar
@@ -1737,35 +1737,32 @@
 marks%E4%BF%A1%E6%81%AF%0D%0A       
- //
  props.setProper
@@ -1795,19 +1795,16 @@
       //
- //
  %E8%AE%BE%E7%BD%AE%E5%8F%AF%E4%BB%A5%E8%8E%B7%E5%8F%96t
@@ -1827,19 +1827,16 @@
 %0A       
- //
  props.s
@@ -1879,16 +1879,92 @@
 true%22);%0D
+%0A        // %E8%AE%BE%E7%BD%AE%E8%BF%9E%E6%8E%A5%E7%BC%96%E7%A0%81%0D%0A        props.setProperty(%22characterEncoding%22, %22utf8%22);%0D
 %0A%0D%0A     
@@ -2435,17 +2435,16 @@
 Object%5B%5D
- 
 %7B%7D));%0D%0A 
@@ -2507,24 +2507,32 @@
  * %E8%8E%B7%E5%8F%96%E6%95%B0%E6%8D%AE%E5%BA%93%E8%BF%9E%E6%8E%A5%0D%0A
+     *%0D%0A
      * @retu

设置连接编码,目前默认utf8