@Test public void test() throws Exception{ //读取信息 InputStream is = ConnectionTest.class.getClassLoader().getResourceAsStream("jdbc.properties"); //properties应放在所在程序Module的src下 Properties pros = new Properties(); pros.load(is); String user = pros.getProperty("user"); String password = pros.getProperty("password"); String url = pros.getProperty("url"); String driverClass = pros.getProperty("driverClass"); //加载Driver Class.forName(driverClass); //获取连接 Connection conn = DriverManager.getConnection(url,user,password); System.out.println(conn); }
Properties文件:
user=root password=root url=jdbc:mysql://localhost:3306/fruitdb?useUnicode=true&characterEncoding=utf-8&useSSL=false driverClass=com.mysql.jdbc.Driver