commons-dbutils工具栏的编写
Posted achengmu
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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; } }
以上是关于commons-dbutils工具栏的编写的主要内容,如果未能解决你的问题,请参考以下文章