String driverClass = null; String jdbcUrl = null; String user = null; String password = null; InputStream inputStream = getClass().getClassLoader().getResourceAsStream("jdbc.properties");//读取外部配置文件 输入流 Properties properties = new Properties(); properties.load(inputStream); driverClass = properties.getProperty("driver"); jdbcUrl = properties.getProperty("jdbcUrl"); user = properties.getProperty("user"); password = properties.getProperty("password"); Driver driver = (Driver)Class.forName(driverClass).newInstance();//通过反射实例化Driver Properties info = new Properties(); info.put("user", user); info.put("password", password); Connection connection = driver.connect(jdbcUrl, info); return connection;
转载于:https://my.oschina.net/u/3554714/blog/2413911