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

準(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ù)連接需要的配置信息聲明在配置文件中讀取配置我呢見,獲取鏈接
/**
* 好處啊
* 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)文章
淺談Java回收對(duì)象的標(biāo)記和對(duì)象的二次標(biāo)記過(guò)程
這篇文章主要介紹了淺談Java回收對(duì)象的標(biāo)記和對(duì)象的二次標(biāo)記過(guò)程的相關(guān)內(nèi)容,小編覺得還是挺不錯(cuò)的,這里給大家分享一下,需要的朋友可以參考。2017-10-10
SpringBoot基于過(guò)濾器和內(nèi)存實(shí)現(xiàn)重復(fù)請(qǐng)求攔截功能
這篇文章主要介紹了SpringBoot基于過(guò)濾器和內(nèi)存實(shí)現(xiàn)重復(fù)請(qǐng)求攔截,這里我們使用過(guò)濾器的方式對(duì)進(jìn)入服務(wù)器的請(qǐng)求進(jìn)行過(guò)濾操作,實(shí)現(xiàn)對(duì)相同客戶端請(qǐng)求同一個(gè)接口的過(guò)濾,需要的朋友可以參考下2023-01-01
MyBatis如何進(jìn)行雙重foreach循環(huán)
這篇文章主要介紹了MyBatis如何進(jìn)行雙重foreach循環(huán),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02
Java語(yǔ)法中Lambda表達(dá)式無(wú)法拋出異常的解決
這篇文章主要介紹了Java語(yǔ)法中Lambda表達(dá)式無(wú)法拋出異常的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09
form-data與x-www-form-urlencoded的區(qū)別以及知識(shí)延伸
這篇文章主要給大家介紹了關(guān)于form-data與x-www-form-urlencoded的區(qū)別以及知識(shí)延伸,form-data和x-www-form-urlencoded都是HTTP請(qǐng)求中用于傳輸表單數(shù)據(jù)的編碼格式,需要的朋友可以參考下2023-11-11

