JDBC鏈接數據庫的幾種方法

1、 最古老的方法(經過 Driver 接口直接鏈接數據庫)java


 

  1. 首先建立一個 Driver 實現類的對象
            Driver dirver = new com.mysql.jdbc.Driver();

     

  2. 準備鏈接數據庫的基本信息:url、user、password
    1     String url = "jdbc:mysql://127.0.0.1:3306/java_jdbc";
    2     Properties info = new Properties();
    3     info.put("user", "root");
    4     info.put("password", "123456");
  3. 調用 Driver 藉口的 connect(url, info) 獲取數據庫鏈接
    1         Connection connection = dirver.connect(url, info);

  實例代碼:mysql

 1         @Test
 2     public void testDriver() throws SQLException {
 3         // 1. 建立一個 Driver 實現類的對象
 4         Driver dirver = new com.mysql.jdbc.Driver();
 5 
 6         // 2. 準備連接數據庫的基本信息:url, user, password
 7         String url = "jdbc:mysql://127.0.0.1:3306/java_jdbc";
 8         Properties info = new Properties();
 9         info.put("user", "root");
10         info.put("password", "123456");
11 
12         // 3. 調用 Driver 藉口的 connect(url, info) 獲取數據庫鏈接
13         Connection connection = dirver.connect(url, info);
14         System.out.println(connection);
15     }
View Code

 


 

2、編寫一個通用的方法(經過Driver 接口實現sql


 

  1. 定義數據庫基本信息變量
    1     String driverClass = null;
    2     String jdbcUrl = null;
    3     String user = null;
    4     String password = null;
  2. 建立一個properties文件,用來存放數據庫鏈接基本信息
    1     driver=com.mysql.jdbc.Driver
    2     jdbcUrl=jdbc:mysql://127.0.0.1:3306/java_jdbc
    3     user=root
    4     password=123456
  3. 讀取類路徑下的jdbc.properties 文件
    1 InputStream in = 
    2                 getClass().getClassLoader().getResourceAsStream("jdbc.properties");
    3 Properties properties = new Properties();
    4 properties.load(in);
  4. 從properties文件獲取數據庫基本信息
    1 driverClass = properties.getProperty("driver");
    2 jdbcUrl = properties.getProperty("jdbcUrl");
    3 user = properties.getProperty("user");
    4 password = properties.getProperty("password");
  5. 經過反射建立 Driver 對象
    1 Driver driver = (Driver) Class.forName(driverClass).newInstance();
  6. 準備用戶名和密碼,並鏈接數據庫
    1 Properties info = new Properties();
    2 info.put("user", user);
    3 info.put("password", password);
    4 Connection connection = driver.connect(jdbcUrl, info);

實例代碼:數據庫

 1     public Connection getConnection() throws Exception {
 2         String driverClass = null;
 3         String jdbcUrl = null;
 4         String user = null;
 5         String password = null;
 6 
 7         // 讀取類路徑下的jdbc.properties 文件
 8         InputStream in = getClass().getClassLoader().getResourceAsStream("jdbc.properties");
 9         Properties properties = new Properties();
10         properties.load(in);
11 
12         driverClass = properties.getProperty("driver");
13         jdbcUrl = properties.getProperty("jdbcUrl");
14         user = properties.getProperty("user");
15         password = properties.getProperty("password");
16 
17         // 經過反射建立 Driver 對象
18         Driver driver = (Driver) Class.forName(driverClass).newInstance();
19 
20         Properties info = new Properties();
21         info.put("user", user);
22         info.put("password", password);
23 
24         // 經過 Driver 的 connect 方法鏈接數據庫.
25         Connection connection = driver.connect(jdbcUrl, info);
26 
27         return connection;
28     }
29 
30     @Test
31     public void testGetConnection() throws Exception {
32         System.out.println(getConnection());
33     }
View Code

 


3、經過DriverManager 驅動管理類獲取數據庫鏈接ide

 


 

  1. 準備數據庫鏈接的四個字符串

 

    1).建立 properties 對象this

1 Properties properties = new Properties();

 

    2).獲取jdbc.properties 對應的輸入流url

1 InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("jdbc.properties");

 

    3).加載對應的輸入流spa

1     properties.load(inputStream);

 

    4).具體決定 user、password、url、driver四個字符串code

1 String driver = properties.getProperty("driver");
2 String jdbcUrl = properties.getProperty("jdbcUrl");
3 String user = properties.getProperty("user");
4 String password = properties.getProperty("password");

  2. 加載數據庫驅動程序對象

1 Class.forName(driver);

 

  3. 經過 Drivermanager 的 getConnection() 方法獲取數據庫鏈接

1 return DriverManager.getConnection(jdbcUrl, user, password);

 

 

實例代碼:

 1     @Test
 2     public void testGetConnection23() throws Exception {
 3         System.out.println(getConnection2());
 4     }
 5     public Connection getConnection2() throws IOException, ClassNotFoundException, SQLException{
 6         
 7         Properties properties = new Properties();
 8         
 9         InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("jdbc.properties");
10         
11         properties.load(inputStream);
12         
13         String driver = properties.getProperty("driver");
14         String jdbcUrl = properties.getProperty("jdbcUrl");
15         String user = properties.getProperty("user");
16         String password = properties.getProperty("password");
17         
18         Class.forName(driver);
19         
20         return DriverManager.getConnection(jdbcUrl, user, password);
21     }
View Code

 


本文只是做者的筆記,只供參考,有錯誤歡迎指出!2017-10-28  11:14:03

相關文章
相關標籤/搜索