java使用JDBC連接數(shù)據(jù)庫(kù)的五種方式(IDEA版)
JDBC是java訪問(wèn)數(shù)據(jù)庫(kù)的基礎(chǔ),其余的mybatis和JDO 以及Hibernate 都是把jdbc封裝起來(lái),因此了解JDBC連接數(shù)據(jù)庫(kù)的原理十分重要?。?/p>

準(zhǔn)備工作
1. mysql的jar包 導(dǎo)入到lib目錄下

2.把導(dǎo)入的jar包添加到項(xiàng)目中
點(diǎn)擊jar包 選擇

3.創(chuàng)建一個(gè)TestConnection類
五種方式如下:
/**
* @author
* @date 2019
**/
import org.junit.Test;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
/**
* JDBC連接
*/
public class ConnectionTest {
//方式一
@Test
public void testConnection1() throws SQLException {
//獲取driver 實(shí)現(xiàn)類的對(duì)象
Driver driver=new com.mysql.jdbc.Driver();
//url;http://localhost:8080/gmall/hello.jpg
String url="jdbc:mysql://localhost:3306/student";
//把數(shù)據(jù)庫(kù)的用戶名和密碼封裝在Properties中
Properties info=new Properties();
info.setProperty("user","root");
info.setProperty("password","root");
// info.setProperty("user","root");
// info.setProperty("password","root");
Connection conn = driver.connect(url, info);
System.out.println(conn);
}
//方式二 對(duì)方式一的迭代 ;不出現(xiàn)第三方的api 是程序又更好的可移植性啊
@Test
public void testConnection2() throws Exception{
//獲取driver實(shí)現(xiàn)類的對(duì)象 反射
Class clazz = Class.forName("com.mysql.jdbc.Driver");
Driver driver=(Driver) clazz.newInstance();
//2.提供要連接的數(shù)據(jù)庫(kù)
String url="jdbc:mysql://localhost:3306/student";
//3.提供用戶密碼
Properties info=new Properties();
info.setProperty("user","root");
info.setProperty("password","root");
//4.獲取鏈接
Connection connect = driver.connect(url, info);
System.out.println(connect);
}
//方式三 使用drivermanager 用來(lái)替換driver
@Test
public void testConneciont3() throws Exception{
//1.獲取Driver的實(shí)現(xiàn)類
Class clazz=Class.forName("com.mysql.jdbc.Driver");
Driver driver=(Driver) clazz.newInstance();
//2. 提供另外三個(gè)獲取連接信息
String url="jdbc:mysql://localhost:3306/student";
String user="root";
String password="root";
//注冊(cè)驅(qū)動(dòng)
DriverManager.registerDriver(driver);
//獲取連接
Connection conn=DriverManager.getConnection(url,user,password);
System.out.println(conn);
}
//方式四
@Test
public void testConneciont4() throws Exception{
//1 提供三個(gè)獲取連接信息
String url="jdbc:mysql://localhost:3306/student";
String user="root";
String password="root";
//2.加載Driver 不用顯示注冊(cè)驅(qū)動(dòng)
Class.forName("com.mysql.jdbc.Driver");
//方式三的優(yōu)化,省略以下操作, Driver的實(shí)現(xiàn)類中自動(dòng)執(zhí)行
// Driver driver=(Driver) clazz.newInstance();
//注冊(cè)驅(qū)動(dòng)
// DriverManager.registerDriver(driver);
//3.獲取連接
Connection conn=DriverManager.getConnection(url,user,password);
System.out.println(conn);
}
//方式五 (final) 將數(shù)據(jù)庫(kù)連接需要的配置信息聲明在配置文件中讀取配置我呢見(jiàn),獲取鏈接
/**
* 好處啊
* 1.實(shí)現(xiàn)了數(shù)據(jù)和代碼的分離,實(shí)現(xiàn)了解耦
* 2,如果需要修改配置文件信息,可以避免程序重新打包
* @throws Exception
*/
@Test
public void TestConnection5() throws Exception{
//讀取配置文件中的信息
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");
//2.加載驅(qū)動(dòng)
Class.forName(driverClass);
//3.獲取鏈接
Connection conn=DriverManager.getConnection(url,user,password);
System.out.println(conn);
}
}
第五種方法
在src 目錄下創(chuàng)建一個(gè) jdbc.properties 文件 內(nèi)容如下

然后每種方式執(zhí)行結(jié)果均為說(shuō)明連接成功?。。?!

到此這篇關(guān)于java使用JDBC連接數(shù)據(jù)庫(kù)的五種方式(IDEA版)的文章就介紹到這了,更多相關(guān)JDBC連接數(shù)據(jù)庫(kù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot使用Druid數(shù)據(jù)源的配置方法
這篇文章主要介紹了SpringBoot使用Druid數(shù)據(jù)源的配置方法,文中代碼實(shí)例相結(jié)合的形式給大家介紹的非常詳細(xì),需要的朋友參考下吧2018-04-04
一文教會(huì)你使用jmap和MAT進(jìn)行堆內(nèi)存溢出分析
本文介紹關(guān)于jmap和MAT的使用來(lái)進(jìn)行堆內(nèi)存溢出分析,因?yàn)檫@個(gè)內(nèi)存溢出是我們手動(dòng)構(gòu)造出來(lái)的,查找比較簡(jiǎn)單,真的到了生產(chǎn)上面需要我們仔細(xì)排除2021-09-09
springboot 使用yml配置文件自定義屬性的操作代碼
在SpringBoot中yml/yaml文件可以自定義一些屬性,以供注入給自定義bean對(duì)象的屬性,主要通過(guò)空格和層次來(lái)實(shí)現(xiàn),類似于python代碼,本文通過(guò)實(shí)例代碼給大家介紹springboot 使用yml配置文件自定義屬性,感興趣的朋友跟隨小編一起看看吧2024-03-03
mybatis中BigDecimal中的0存為null的坑及解決
在使用MyBatis進(jìn)行數(shù)據(jù)庫(kù)操作時(shí),若Java中屬性類型為BigDecimal且值為0,插入數(shù)據(jù)庫(kù)時(shí)可能會(huì)變?yōu)閚ull,而不是0,這個(gè)問(wèn)題可能是由于MyBatis在處理BigDecimal類型時(shí)的弱類型判斷導(dǎo)致的,當(dāng)BigDecimal變量與空字符串進(jìn)行比較時(shí),MyBatis可能將其視為null2024-10-10
SpringBoot訪問(wèn)外部文件及默認(rèn)路由問(wèn)題
這篇文章主要介紹了SpringBoot訪問(wèn)外部文件及默認(rèn)路由問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11
微服務(wù)Redis-Session共享登錄狀態(tài)的過(guò)程詳解
這篇文章主要介紹了微服務(wù)Redis-Session共享登錄狀態(tài),本文采取Spring security做登錄校驗(yàn),用redis做session共享,實(shí)現(xiàn)單服務(wù)登錄可靠性,微服務(wù)之間調(diào)用的可靠性與通用性,需要的朋友可以參考下2023-12-12
基于java下載中g(shù)etContentLength()一直為-1的一些思路
下面小編就為大家?guī)?lái)一篇基于java下載中g(shù)etContentLength()一直為-1的一些思路。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-06-06

