标签:
//第一种方式 Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb?user=root&password=root") ;
//第二种方式 //读取properties文件 Properties pro = new Properties() ; InputStream in = JdbcDemo3.class.getClassLoader().getResourceAsStream("dbcfg.properties") ; pro.load(in) ; Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", pro) ;
//第三种方式 Connection conn = DriverManager.getConnection(url,user,password) ;
输入文件名和扩展名(.properties)
private static String driverClass = "" ; private static String url = "" ; private static String user = "" ; private static String password = ""; static{ ResourceBundle rb = ResourceBundle.getBundle("dbcfg") ; driverClass = rb.getString("driverClass") ; url = rb.getString("url") ; user = rb.getString("user") ; password = rb.getString("password") ; try { Class.forName(driverClass) ;//自动注册驱动 } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static Connection getConnection(){ try { return DriverManager.getConnection(url, user, password) ; } catch (SQLException e) { e.printStackTrace(); } return null ; }
标签:
原文地址:http://www.cnblogs.com/xiezie/p/5689410.html