日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

mysql drivermanager_MYSQL 之 JDBC(二): 数据库连接(二)通过DriverManager获取数据库连接...

發(fā)布時間:2024/4/11 数据库 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mysql drivermanager_MYSQL 之 JDBC(二): 数据库连接(二)通过DriverManager获取数据库连接... 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

通過DriverManager獲取數(shù)據(jù)庫連接

修改一下配置文件

driver=com.mysql.cj.jdbc.Driver

jdbcUrl=jdbc:mysql://localhost:3306/testjdbc?serverTimezone=GMT%2B8

user=root

password=123456

代碼(我覺得廢話有點多,同一個知識點翻來覆去的講,并且有的疑點還沒解決)

比如說:利用Driver和DriverManager都能用不同的數(shù)據(jù)庫,為什么DriverManager更好

package com.litian.jdbc;

import java.io.InputStream;

import java.sql.Connection;

import java.sql.Driver;

import java.sql.DriverManager;

import java.sql.SQLException;

import java.util.Properties;

/**

* @author: Li Tian

* @contact: litian_cup@163.com

* @software: IntelliJ IDEA

* @file: JDBCTest.java

* @time: 2019/12/15 18:56

* @desc: JDBC試驗,Driver是一個接口:數(shù)據(jù)庫廠商必須提供實現(xiàn)的接口,能從其中獲取數(shù)據(jù)庫連接。

*/

public class JDBCTest {

public Connection getConnection2() throws Exception {

// 1. 準備連接數(shù)據(jù)庫的4個字符串。

// 1.1 創(chuàng)建Properties對象

Properties properties = new Properties();

// 1.2 獲取jdbc.properties對應(yīng)的輸入流

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

// 1.3 加載1.2對應(yīng)的輸入流

properties.load(in);

// 1.4 具體決定user,password等4個字符串。

String user = properties.getProperty("user");

String password = properties.getProperty("password");

String jdbcUrl = properties.getProperty("jdbcUrl");

String driver = properties.getProperty("driver");

// 2. 加載數(shù)據(jù)庫驅(qū)動程序

Class.forName(driver);

// 3. 通過DriverManager的getConnection()方法獲取數(shù)據(jù)庫連接。

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

}

/**

* DriverManager是驅(qū)動的管理類

* 1. 可以通過重載的getConnection()方法獲取數(shù)據(jù)庫連接。較為方便

* 2. 可以同時管理多個驅(qū)動程序:若注冊了多個數(shù)據(jù)庫連接,則調(diào)動getConnection()方法時

* 傳入的參數(shù)不同,則返回不同的數(shù)據(jù)庫連接

*/

public void testDriverManager() throws Exception {

// 1. 準備連接數(shù)據(jù)庫的4個字符串

// 驅(qū)動的全類名

String driverClass = null;

String jdbcUrl = null;

String user = null;

String password = null;

// 讀取類路徑下的jdbc.propertites 文件

InputStream in = getClass().getClassLoader().getResourceAsStream("jdbc.properties");

Properties properties = new Properties();

properties.load(in);

driverClass = properties.getProperty("driver");

jdbcUrl = properties.getProperty("jdbcUrl");

user = properties.getProperty("user");

password = properties.getProperty("password");

// 2. 加載數(shù)據(jù)庫驅(qū)動程序(對應(yīng)的Driver實現(xiàn)類中有注冊驅(qū)動的靜態(tài)代碼塊程序)

// 下面的注冊程序已經(jīng)寫好了,不需要自己寫

// DriverManager.registerDriver((Driver) Class.forName(driverClass).newInstance());

Class.forName(driverClass);

// 3. 通過DriverManager的getConnection()方法獲取數(shù)據(jù)庫連接

Connection connection = DriverManager.getConnection(jdbcUrl, user, password);

System.out.println(connection);

}

public void test1() throws SQLException {

// 1. 創(chuàng)建一個Driver實現(xiàn)類的對象

Driver driver = new com.mysql.jdbc.Driver();

// 2. 準備連接數(shù)據(jù)庫的基本信息:url,user,password

String url = "jdbc:mysql://localhost:3306/girls";

Properties info = new Properties();

info.put("user", "root");

info.put("password", "tian19951103");

// 3. 調(diào)用Driver接口的connect(url, info)獲取數(shù)據(jù)庫連接

Connection connection = driver.connect(url, info);

System.out.println(connection);

}

// 編寫一個通用的方法,在不修改源程序的情況下,可以獲取任何數(shù)據(jù)庫的連接

public Connection getConnection() throws Exception {

String driverClass = null;

String jdbcUrl = null;

String user = null;

String password = null;

// 讀取類路徑下的jdbc.propertites 文件

InputStream in = getClass().getClassLoader().getResourceAsStream("jdbc.properties");

Properties properties = new Properties();

properties.load(in);

driverClass = properties.getProperty("driver");

jdbcUrl = properties.getProperty("jdbcUrl");

user = properties.getProperty("user");

password = properties.getProperty("password");

Driver driver = (Driver) Class.forName(driverClass).newInstance();

Properties info = new Properties();

info.put("user", user);

info.put("password", password);

Connection connection = driver.connect(jdbcUrl, info);

return connection;

}

public void testGetConnection() throws Exception {

System.out.println(getConnection());

}

public static void main(String[] args) throws Exception {

// new JDBCTest().testGetConnection();

// new JDBCTest().testDriverManager();

Connection conn = new JDBCTest().getConnection2();

System.out.println(conn);

}

}

超強干貨來襲 云風專訪:近40年碼齡,通宵達旦的技術(shù)人生

總結(jié)

以上是生活随笔為你收集整理的mysql drivermanager_MYSQL 之 JDBC(二): 数据库连接(二)通过DriverManager获取数据库连接...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。