JDBC技術(shù)-獲取數(shù)據(jù)庫的連接

1.加載數(shù)據(jù)庫驅(qū)動

(1)在工程目錄下新建Floder->Folder name:lib

(2)在lib目錄導(dǎo)入數(shù)據(jù)庫驅(qū)動.jar包,再選擇Build Path->Add to Build Path,最后才能使用.jar包下的API。

(3)加載驅(qū)動源碼(驅(qū)動文件下src文件目錄)

之后選中src文件即可。

2.獲取數(shù)據(jù)庫連接方式一:

@Test

public void testConnection1() throws SQLException {

// 獲取Driver實(shí)現(xiàn)類對象

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

// url:http://localhost:8080/gmall/keyboard.jpg

// jdbc:mysql:協(xié)議

// localhost:ip地址

// 3306:默認(rèn)mysql的端口號

// test:test數(shù)據(jù)庫

String url = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8";

// 將用戶名和密碼封裝在Properties中

Properties info = new Properties();

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

info.setProperty("password", "123456");

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

System.out.println(conn);

}

①Eclipse連接數(shù)據(jù)庫報錯com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown database 'test'.

背景:用戶名密碼正確,database在mysql中存在。

命令行下執(zhí)行show databases(),test數(shù)據(jù)庫是存在的。

找資料發(fā)現(xiàn)有可能是有其他mysql服務(wù)自啟動了,導(dǎo)致程序有點(diǎn)混亂。

鍵盤“windows”+R 輸入services.msc

發(fā)現(xiàn)有mysql這個服務(wù)是啟動的。關(guān)掉即可。

②Eclipse連接數(shù)據(jù)庫報錯:Unknown initial character set index '255' received from server.

java.sql.SQLException: Unknown initial character set index ‘255’ received from server.

編碼不匹配的原因

方法一:直接在連接的URL后加上

?useUnicode=true&characterEncoding=utf8 即可。

方法二:換一個新版本的mysql-connector-java。

5.1.44的character_set_server和character_set_database系統(tǒng)變量的默認(rèn)值已從latin1更改為utf8mb4。

mysql 8.0版本與mysql數(shù)據(jù)庫驅(qū)動8.0以上版本

第一步:把驅(qū)動的類名改為:

static String driver="com.mysql.cj.jdbc.Driver";

第二步:在訪問mysql的url后加入時區(qū)設(shè)置:

static String url="jdbc:mysql://localhost:3306/test?characterEncoding=utf-8&serverTimezone=UTC";(UTC表示標(biāo)準(zhǔn)時區(qū))

Druid數(shù)據(jù)庫連接池技術(shù)的實(shí)現(xiàn)

①在WEB-INF目錄下創(chuàng)建一個lib目錄,把mysql數(shù)據(jù)庫驅(qū)動,Druid數(shù)據(jù)庫連接池驅(qū)動放到lib目錄下,并加載到lib。

②編寫JDBCUtils類,實(shí)現(xiàn)數(shù)據(jù)庫連接池技術(shù)連接數(shù)據(jù)庫。

編寫druid.properties配置文件。

public class JDBCUtils {

? ? /*public static void main(String[] args) {

? ? ? ? try {

? ? ? ? ? ? Connection conn = JDBCUtils.getConnection();//每調(diào)用一次getConnection()就創(chuàng)建一個數(shù)據(jù)庫連接池

? ? ? ? ? ? System.out.println(conn);

? ? ? ? } catch (Exception e) {

? ? ? ? ? ? e.printStackTrace();

? ? ? ? }

? ? }*/

? ? /**

? ? * 獲取數(shù)據(jù)庫連接池中的連接

? ? * @return

? ? */

? ? private static DataSource dataSource;

? ? //使用靜態(tài)代碼塊初始化數(shù)據(jù)庫連接池

? ? static {

? ? ? ? try {

? ? ? ? ? ? Properties pros = new Properties();

? ? ? ? ? ? //InputStream is = JDBCUtils.class.getClassLoader().getResourceAsStream("jdbc.properties");

? ? ? ? ? ? //讀取jdbc.properties屬性配置文件

? ? ? ? ? ? InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("jdbc.properties");

? ? ? ? ? ? //從流中加載數(shù)據(jù)

? ? ? ? ? ? pros.load(is);

? ? ? ? ? ? //創(chuàng)建數(shù)據(jù)庫連接池

? ? ? ? ? ? dataSource = DruidDataSourceFactory.createDataSource(pros);

? ? ? ? } catch (Exception e) {

? ? ? ? ? ? e.printStackTrace();

? ? ? ? }

? ? }

? ? public static Connection getConnection(){

? ? ? ? Connection conn = null;

? ? ? ? try {

? ? ? ? ? ? conn = dataSource.getConnection();

? ? ? ? } catch (SQLException e) {

? ? ? ? ? ? e.printStackTrace();

? ? ? ? }

? ? ? ? return conn;

? ? }

? /* public static Connection getConnection() throws Exception {

? ? ? ? InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("jdbc.properties");

? ? ? ? //FileInputStream is = new FileInputStream(new File("src/jdbc.properties"));

? ? ? ? Properties pros = new Properties();

? ? ? ? pros.load(is);

? ? ? ? //創(chuàng)建一個Druid數(shù)據(jù)庫連接池

? ? ? ? DataSource dataSource = DruidDataSourceFactory.createDataSource(pros);

? ? ? ? Connection conn = dataSource.getConnection();

? ? ? ? return conn;

? ? }*/

? ? /**

? ? * 關(guān)閉連接,放回數(shù)據(jù)庫連接池

? ? * @param conn

? ? */

? ? public static void close(Connection conn){

? ? ? ? if (conn != null);

? ? ? ? try {

? ? ? ? ? ? conn.close();

? ? ? ? } catch (SQLException e) {

? ? ? ? ? ? e.printStackTrace();

? ? ? ? }

? ? }

}

public class JDBCUtilsTest {

? ? @Test

? ? public void test(){

? ? ? ? Connection conn = JDBCUtils.getConnection();

? ? ? ? System.out.println(conn);

? ? ? ? JDBCUtils.close(conn);

? ? }

}

使用數(shù)據(jù)庫連接池獲取數(shù)據(jù)庫連接,連接使用完一定要記得及時關(guān)閉數(shù)據(jù)庫連接。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容