新建Maven工程
pom.xml
<dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.15</version> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>5.4.0</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <!-- 指定jdk --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build>
數據庫信息
driver=com.mysql.cj.jdbc.Driver jdbcUrl=jdbc:mysql://192.168.8.136:3306/jdbc user=root password=root
獲取鏈接
Driver
/** * Driver 是一個接口: 可以通過 Driver 的實現類對象獲取數據庫連接 */ @Test public void testDriver() throws SQLException { // 創建 Driver 實現類對象 Driver driver = new com.mysql.cj.jdbc.Driver(); // 准備連接數據庫信息: url, user, password String url = "jdbc:mysql://192.168.8.136:3306/jdbc"; Properties info = new Properties(); info.put("user", "root"); info.put("password", "root"); // 調用 Driver 接口的 connect(url, info) 獲取數據庫連接 Connection connection = driver.connect(url, info); System.out.println(connection); } @Test public void getConnection() throws Exception{ // 讀取類路徑下的 jdbc.properties 文件 InputStream in = getClass().getClassLoader().getResourceAsStream("jdbc.properties"); Properties properties = new Properties(); properties.load(in); String driverClass = properties.getProperty("driver"); String jdbcUrl = properties.getProperty("jdbcUrl"); String user = properties.getProperty("user"); String password = properties.getProperty("password"); //通過反射 Driver 對象 Driver driver = (Driver) Class.forName(driverClass).newInstance(); Properties info = new Properties(); info.put("user", user); info.put("password", password); //通過 Driver 的 connect 方法獲取數據庫連接 Connection connection = driver.connect(jdbcUrl, info); System.out.println(connection); }
DriverManager
/** * DriverManager 是驅動的管理類 * 1). 可以通過重載的 getConnection() 方法獲取數據庫連接. 較為方便 * 2). 可以同時管理多個驅動程序: 若注冊了多個數據庫連接, 則調用 getConnection() * @throws Exception */ @Test public void testDriverManager() throws Exception{ // 准備連接數據庫 String driverClass = "com.mysql.cj.jdbc.Driver"; String jdbcUrl = "jdbc:mysql://192.168.8.136/jdbc"; String user = "root"; String password = "root"; // 加載數據庫驅動程序(對應的 Driver 實現類中有注冊驅動的靜態代碼塊 Class.forName(driverClass); // 通過 DriverManager 的 getConnection() 方法獲取數據庫連接 Connection connection = DriverManager.getConnection(jdbcUrl, user, password); System.out.println(connection); } @Test public void getConnection2() throws Exception{ Properties properties = new Properties(); InputStream in = this.getClass().getClassLoader().getResourceAsStream("jdbc.properties"); properties.load(in); String driver = properties.getProperty("driver"); String jdbcUrl = properties.getProperty("jdbcUrl"); String user = properties.getProperty("user"); String password = properties.getProperty("password"); // 加載數據庫驅動程序(對應的 Driver 實現類中有注冊驅動的靜態代碼塊) Class.forName(driver); // 通過 DriverManager 的 getConnection() 方法獲取數據庫連接. Connection connection = DriverManager.getConnection(jdbcUrl, user, password); System.out.println(connection); }