您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“IDEA軟件中怎么實現(xiàn)Java使用JDBC連接數(shù)據(jù)庫”的有關(guān)知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
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);
}
}
“IDEA軟件中怎么實現(xiàn)Java使用JDBC連接數(shù)據(jù)庫”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。