commons-dbutils工具栏的编写

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;
    }
}

  

猜你喜欢

转载自www.cnblogs.com/achengmu/p/10911083.html
今日推荐