java使用JDBC連接數(shù)據(jù)庫的五種方式(IDEA版)
JDBC是java訪問數(shù)據(jù)庫的基礎(chǔ),其余的mybatis和JDO 以及Hibernate 都是把jdbc封裝起來,因此了解JDBC連接數(shù)據(jù)庫的原理十分重要??!
準(zhǔn)備工作
1. mysql的jar包 導(dǎo)入到lib目錄下
2.把導(dǎo)入的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"; //把數(shù)據(jù)庫的用戶名和密碼封裝在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.提供要連接的數(shù)據(jù)庫 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"; //注冊驅(qū)動 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 不用顯示注冊驅(qū)動 Class.forName("com.mysql.jdbc.Driver"); //方式三的優(yōu)化,省略以下操作, Driver的實現(xiàn)類中自動執(zhí)行 // Driver driver=(Driver) clazz.newInstance(); //注冊驅(qū)動 // DriverManager.registerDriver(driver); //3.獲取連接 Connection conn=DriverManager.getConnection(url,user,password); System.out.println(conn); } //方式五 (final) 將數(shù)據(jù)庫連接需要的配置信息聲明在配置文件中讀取配置我呢見,獲取鏈接 /** * 好處啊 * 1.實現(xiàn)了數(shù)據(jù)和代碼的分離,實現(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ū)動 Class.forName(driverClass); //3.獲取鏈接 Connection conn=DriverManager.getConnection(url,user,password); System.out.println(conn); } }
第五種方法
在src 目錄下創(chuàng)建一個 jdbc.properties 文件 內(nèi)容如下
然后每種方式執(zhí)行結(jié)果均為說明連接成功?。。?!
到此這篇關(guān)于java使用JDBC連接數(shù)據(jù)庫的五種方式(IDEA版)的文章就介紹到這了,更多相關(guān)JDBC連接數(shù)據(jù)庫內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
淺談Java回收對象的標(biāo)記和對象的二次標(biāo)記過程
這篇文章主要介紹了淺談Java回收對象的標(biāo)記和對象的二次標(biāo)記過程的相關(guān)內(nèi)容,小編覺得還是挺不錯的,這里給大家分享一下,需要的朋友可以參考。2017-10-10SpringBoot基于過濾器和內(nèi)存實現(xiàn)重復(fù)請求攔截功能
這篇文章主要介紹了SpringBoot基于過濾器和內(nèi)存實現(xiàn)重復(fù)請求攔截,這里我們使用過濾器的方式對進入服務(wù)器的請求進行過濾操作,實現(xiàn)對相同客戶端請求同一個接口的過濾,需要的朋友可以參考下2023-01-01form-data與x-www-form-urlencoded的區(qū)別以及知識延伸
這篇文章主要給大家介紹了關(guān)于form-data與x-www-form-urlencoded的區(qū)別以及知識延伸,form-data和x-www-form-urlencoded都是HTTP請求中用于傳輸表單數(shù)據(jù)的編碼格式,需要的朋友可以參考下2023-11-11