当前位置:Gxlcms > 数据库问题 > commons-dbutils工具栏的编写

commons-dbutils工具栏的编写

时间:2021-07-01 10:21:17 帮助过:4人阅读

db.properties

driverClass=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test_db?useUnicode=true&characterEncoding=UTF-8&jdbcCompliantTruncation=false
user=root
password=

  

commonsDbutils工具类

public class CommonsDbutils {

    private static Connection conn;

    private static String driverClass;

    private static String url;

    private static String user;

    private static String password;

    static {

        try {
            readDBConfig();
            Class.forName(driverClass);
            conn = DriverManager.getConnection(url, user, password);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static void readDBConfig(){
        try {
            InputStream in = CommonsDbutils.class.getClassLoader().getResourceAsStream("db.properties");
            Properties pro = new Properties();
            pro.load(in);
            driverClass = pro.getProperty("driverClass");
            url = pro.getProperty("url");
            user = pro.getProperty("user");
            password = pro.getProperty("password");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static Connection getConnection(){
        return conn;
    }
}

  

commons-dbutils工具栏的编写

标签:rop   resource   stream   oid   unicode   password   pass   vat   als   

人气教程排行