Java連接Oracle數(shù)據(jù)庫(kù)完整步驟記錄
第一步:導(dǎo)入jar包
commons-dbutils-1.6.jar,jar包大家就到網(wǎng)上搜吧

第二步:配置數(shù)據(jù)庫(kù)參數(shù)
Oracle配置參數(shù):xml中配置
<user mark="{VE}" desc="數(shù)據(jù)庫(kù)用戶">root</user>
<password mark="{VE}" desc="數(shù)據(jù)庫(kù)密碼">lyt\!QAZ123456</password>
<url mark="{VE}" desc="數(shù)據(jù)庫(kù)url">jdbc:oracle:thin:@//127.0.0.1:1521/OADB</url>
<driverClass mark="{VE}" desc="驅(qū)動(dòng)">oracle.jdbc.driver.OracleDriver</driverClass>重點(diǎn)一:Oracle數(shù)據(jù)庫(kù)的JDBC連接分為三種,服務(wù)名(SERVICE_NAME)、SID和TNSName三種,所以在寫數(shù)據(jù)庫(kù)的url一定要先確認(rèn)是那種JDBC連接,下面給出三種JDBC連接的書寫方式:
SERVICE_NAME方式:
jdbc:oracle:thin:@//<host>:<port>/<SERVICE_NAME>
SID方式:
jdbc:oracle:thin:@<host>:<port>:<SID> 或:jdbc:oracle:thin:@<host>:<port>/<SID>
TNSName連接方式:
jdbc:oracle:thin:@<TNSName>
重點(diǎn)二:oracle的密碼中包含!、@、#等時(shí)需要轉(zhuǎn)移
例如我的密碼是lyt!QAZ123456 這個(gè)時(shí),在配置文件中要加入\進(jìn)行轉(zhuǎn)移,即書寫為lyt\!QAZ123456
Mysql配置參數(shù):xml方式配置
<user mark="{VE}" desc="數(shù)據(jù)庫(kù)用戶">root</user>
<password mark="{VE}" desc="數(shù)據(jù)庫(kù)密碼">lyt123456</password>
<url mark="{VE}" desc="數(shù)據(jù)庫(kù)url">jdbc:mysql://127.0.0.1:3306/test3?useUnicode=true&characterEncoding=UTF-8&userSSL=false&serverTimezone=GMT%2B8</url>
<driverClass mark="{VE}" desc="驅(qū)動(dòng)">com.mysql.jdbc.Driver</driverClass>重點(diǎn):數(shù)據(jù)庫(kù)url中如果出現(xiàn)&符號(hào)時(shí),需要轉(zhuǎn)義為&
第三步:寫一個(gè)工具類JDBCUtils
可以直接拿來(lái)用的工具類~~
import com.seeyon.ctp.common.log.CtpLogFactory;
import com.seeyon.ctp.rest.resources.MySSOResources;
import org.apache.commons.logging.Log;
import java.sql.*;
/**
* @author : lvyitingx
* @date : 2023-04-03 16:00
**/
public class JDBCUtils {
private static final Log logger = CtpLogFactory.getLog(JDBCUtils.class);
private static final String user = System.getProperty("xnsy.membersync.user");
private static final String password = System.getProperty("xnsy.membersync.password");
private static final String url = System.getProperty("xnsy.membersync.url");
private static final String driverClass = System.getProperty("xnsy.membersync.driverClass");
public static Connection getConnection(){
Connection con = null;
try {
//獲取參數(shù)
logger.info("數(shù)據(jù)庫(kù)參數(shù):"+user +password +url +driverClass);
//2.加載驅(qū)動(dòng)
Class.forName(driverClass);
//3.獲取連接
con = DriverManager.getConnection(url,user,password);
} catch (Exception e) {
logger.info("數(shù)據(jù)庫(kù)連接錯(cuò)誤"+e);
throw new RuntimeException(e);
}
return con;
}
/**
* 關(guān)閉連接、Statement和ResultSet
* @param con
* @param ps
*/
public static void closeResource(Connection con, Statement ps, ResultSet rs) {
//資源關(guān)閉
try {
if(ps != null)
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
try {
if(con != null)
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
try {
if(rs != null)
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}第四步:連接數(shù)據(jù)庫(kù)
@Path("updateMember")
@GET
public void updateMember() {
Connection con = null;
PreparedStatement ps = null;
ResultSet resultSet = null;
List<String> midMembers = null;
try {
midMembers = new ArrayList<>();
con = JDBCUtils.getConnection();
String sql = "select code from mid_org_person";
ps = con.prepareStatement(sql);
resultSet = ps.executeQuery();
logger.info("獲取結(jié)果成功"+resultSet);
//獲取中間表人員編號(hào)
while (resultSet.next()) {
midMembers.add(resultSet.getString(1));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
//關(guān)閉資源
JDBCUtils.closeResource(con, ps, resultSet);
}
}到這里就可以連接成功啦~~
總結(jié)
到此這篇關(guān)于Java連接Oracle數(shù)據(jù)庫(kù)的文章就介紹到這了,更多相關(guān)Java連接Oracle數(shù)據(jù)庫(kù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
IntelliJIDEA中實(shí)現(xiàn)SpringBoot多實(shí)例運(yùn)行的兩種方式
在微服務(wù)開發(fā)中,經(jīng)常需要同時(shí)啟動(dòng)多個(gè)服務(wù)實(shí)例進(jìn)行測(cè)試或模擬集群環(huán)境,?IntelliJ?IDEA?作為Java開發(fā)者常用工具,提供了靈活的多實(shí)例啟動(dòng)支持,本文將詳細(xì)介紹如何通過(guò)修改配置?和批量啟動(dòng)?兩種方式實(shí)現(xiàn)SpringBoot多實(shí)例運(yùn)行,并解決常見問題,需要的朋友可以參考下2025-03-03
Java中文件創(chuàng)建于寫入內(nèi)容的常見方法
在日常開發(fā)中,肯定離不開要和文件打交道,今天就簡(jiǎn)單羅列一下平時(shí)比較常用的創(chuàng)建文件并向文件中寫入數(shù)據(jù)的幾種方式,希望對(duì)大家有一定的幫助2023-10-10
JavaWeb之Servlet注冊(cè)頁(yè)面的實(shí)現(xiàn)示例
注冊(cè)頁(yè)面是很多網(wǎng)站都會(huì)是使用的到,本文主要介紹了JavaWeb之Servlet注冊(cè)頁(yè)面的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04
SpringDataJPA實(shí)體類關(guān)系映射配置方式
這篇文章主要介紹了SpringDataJPA實(shí)體類關(guān)系映射配置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12
Java中線程執(zhí)行狀態(tài)檢測(cè)的四種可靠方法
在多線程開發(fā)中,開發(fā)者常需監(jiān)控子線程狀態(tài),這個(gè)問題關(guān)系到系統(tǒng)的可靠性和錯(cuò)誤處理能力,需要一套完善的方案來(lái)解決,下面我們來(lái)看看四種從基礎(chǔ)到高級(jí)的解決方案吧2025-05-05

