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;