1.JDBC體系系統 一組規范:介面
- JDBC介面(API)包括兩個層次:
- 面向應用的API:Java API,抽象介面,供應用開發人員使用(連接資料庫,執行SQL陳述句,獲得結果)
- 面向資料庫的API:Java Driver API,供開發商開發資料庫驅動程式
JDBC是sun公司提供一套用于資料庫操作的介面,java程式員只需要面向這套介面編程即可,
不同的資料庫廠商,需要針對這套介面,提供不同的實作集合,即為不同資料庫的驅動
package com.aiguigu.connection;
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
public class ConnectionTest {
@Test
public void testConnection1() throws SQLException {
Driver driver = new com.mysql.cj.jdbc.Driver();
//jdbc:mysql協議
//local:IP地址
//3306:默認mysql埠號
//test:test資料庫
String url = "jdbc:mysql://localhost:3306/MYSQL?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone = GMT";
//將用戶名和密碼封裝在properties中
Properties info = new Properties();
info.setProperty("user","root");
info.setProperty("password","1234");
Connection conn = driver.connect(url, info);
System.out.println(conn);
}
//方式二:對方式一的迭代 在如下的方式中不出現第三方API,使程式具有更好的可移植性
@Test
public void testConnection2() throws Exception {
//獲取Driver實作類物件,使用反射
Class<?> clazz = Class.forName("com.mysql.cj.jdbc.Driver");
Driver driver = (Driver) clazz.newInstance();
//2.提供要連接的資料庫
String url = "jdbc:mysql://localhost:3306/MYSQL?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone = GMT";
//提供要連接的用戶名和密碼
Properties info = new Properties();
info.setProperty("user","root");
info.setProperty("password","1234");
//獲取連接
Connection conn = driver.connect(url, info);
System.out.println(conn);
}
@Test
//方式三:使用DriverManager替換Driver
public void testConnection3() throws Exception {
//1.獲取Drive實作類的物件
Class<?> clazz = Class.forName("com.mysql.cj.jdbc.Driver");
Driver driver = (Driver) clazz.newInstance();
//2提供另外三個連接的基本資訊
String url = "jdbc:mysql://localhost:3306/MYSQL?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone = GMT";
String user = "root";
String password = "1234";
//注冊驅動
DriverManager.registerDriver(driver);
//獲取連接
Connection connection = DriverManager.getConnection(url, user, password);
System.out.println(connection);
}
@Test
//方式三:使用DriverManager替換Driver
public void testConnection4() throws Exception {
//1.提供另外三個連接的基本資訊
String url = "jdbc:mysql://localhost:3306/MYSQL?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone = GMT";
String user = "root";
String password = "1234";
//2.獲取Drive實作類的物件
Class.forName("com.mysql.cj.jdbc.Driver");
//相較于方式三可以省略如下操作:
// Driver driver = (Driver) clazz.newInstance();
// //注冊驅動
// DriverManager.registerDriver(driver);
//3.獲取連接
Connection connection = DriverManager.getConnection(url, user, password);
System.out.println(connection);
}
//方式五:將資料庫連接的四個基本資訊宣告在組態檔中通過讀取組態檔,獲取連接
@Test
public void getConnection5() throws Exception {
//1.讀取組態檔中4個基本資訊
InputStream is = ConnectionTest.class.getClassLoader().getResourceAsStream("jdbc.properties");
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");
//加載驅動
Class.forName(driverClass);
//獲取連接
Connection conn = DriverManager.getConnection(url, user, password);
System.out.println(conn);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/501654.html
標籤:其他
上一篇:ETCD快速入門-03 常用命令
