package 配置方式.dbUtils; import java.io.InputStream; import java.sql.Connection; import java.sql.DriverManager; import java.util.Properties; public class Connection2 { // 声明静态con private static Connection con; static { try { // 声明一个资源文件 Properties pro = new Properties(); // 载入 InputStream in = Connection2.class .getResourceAsStream("jdbc.properties"); pro.load(in); // 获取对应值 String driver = pro.getProperty("driver"); String url = pro.getProperty("url"); String name = pro.getProperty("username"); String pwd = pro.getProperty("password"); Class.forName(driver); con = DriverManager.getConnection(url, name, pwd); } catch (Exception e) { } } private Connection2() { } public static Connection GetCon() { return con; } public static void main(String[] args) { System.err.println(Connection2.GetCon()); } }
转载于:https://www.cnblogs.com/xiaweifeng/p/3688902.html