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

準備工作
1. mysql的jar包 導入到lib目錄下

2.把導入的jar包添加到項目中
點擊jar包 選擇

3.創(chuàng)建一個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 實現(xiàn)類的對象
Driver driver=new com.mysql.jdbc.Driver();
//url;http://localhost:8080/gmall/hello.jpg
String url="jdbc:mysql://localhost:3306/student";
//把數據庫的用戶名和密碼封裝在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);
}
//方式二 對方式一的迭代 ;不出現(xiàn)第三方的api 是程序又更好的可移植性啊
@Test
public void testConnection2() throws Exception{
//獲取driver實現(xiàn)類的對象 反射
Class clazz = Class.forName("com.mysql.jdbc.Driver");
Driver driver=(Driver) clazz.newInstance();
//2.提供要連接的數據庫
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 用來替換driver
@Test
public void testConneciont3() throws Exception{
//1.獲取Driver的實現(xiàn)類
Class clazz=Class.forName("com.mysql.jdbc.Driver");
Driver driver=(Driver) clazz.newInstance();
//2. 提供另外三個獲取連接信息
String url="jdbc:mysql://localhost:3306/student";
String user="root";
String password="root";
//注冊驅動
DriverManager.registerDriver(driver);
//獲取連接
Connection conn=DriverManager.getConnection(url,user,password);
System.out.println(conn);
}
//方式四
@Test
public void testConneciont4() throws Exception{
//1 提供三個獲取連接信息
String url="jdbc:mysql://localhost:3306/student";
String user="root";
String password="root";
//2.加載Driver 不用顯示注冊驅動
Class.forName("com.mysql.jdbc.Driver");
//方式三的優(yōu)化,省略以下操作, Driver的實現(xiàn)類中自動執(zhí)行
// Driver driver=(Driver) clazz.newInstance();
//注冊驅動
// DriverManager.registerDriver(driver);
//3.獲取連接
Connection conn=DriverManager.getConnection(url,user,password);
System.out.println(conn);
}
//方式五 (final) 將數據庫連接需要的配置信息聲明在配置文件中讀取配置我呢見,獲取鏈接
/**
* 好處啊
* 1.實現(xiàn)了數據和代碼的分離,實現(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.加載驅動
Class.forName(driverClass);
//3.獲取鏈接
Connection conn=DriverManager.getConnection(url,user,password);
System.out.println(conn);
}
}
第五種方法
在src 目錄下創(chuàng)建一個 jdbc.properties 文件 內容如下

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

到此這篇關于java使用JDBC連接數據庫的五種方式(IDEA版)的文章就介紹到這了,更多相關JDBC連接數據庫內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
springboot 使用yml配置文件自定義屬性的操作代碼
在SpringBoot中yml/yaml文件可以自定義一些屬性,以供注入給自定義bean對象的屬性,主要通過空格和層次來實現(xiàn),類似于python代碼,本文通過實例代碼給大家介紹springboot 使用yml配置文件自定義屬性,感興趣的朋友跟隨小編一起看看吧2024-03-03
mybatis中BigDecimal中的0存為null的坑及解決
在使用MyBatis進行數據庫操作時,若Java中屬性類型為BigDecimal且值為0,插入數據庫時可能會變?yōu)閚ull,而不是0,這個問題可能是由于MyBatis在處理BigDecimal類型時的弱類型判斷導致的,當BigDecimal變量與空字符串進行比較時,MyBatis可能將其視為null2024-10-10
微服務Redis-Session共享登錄狀態(tài)的過程詳解
這篇文章主要介紹了微服務Redis-Session共享登錄狀態(tài),本文采取Spring security做登錄校驗,用redis做session共享,實現(xiàn)單服務登錄可靠性,微服務之間調用的可靠性與通用性,需要的朋友可以參考下2023-12-12
基于java下載中getContentLength()一直為-1的一些思路
下面小編就為大家?guī)硪黄趈ava下載中getContentLength()一直為-1的一些思路。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-06-06

