码迷,mamicode.com
首页 > 数据库 > 详细

commons-dbutils工具栏的编写

时间:2019-05-23 12:12:43      阅读:120      评论:0      收藏:0      [点我收藏+]

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

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   

原文地址:https://www.cnblogs.com/achengmu/p/10911083.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!