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)文章
Java tomcat環(huán)境變量及idea配置解析
這篇文章主要介紹了Java tomcat環(huán)境變量及idea配置解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-12-12Jrebel License Server 激活 IDEA-Jrebel-在線-
這篇文章主要介紹了Jrebel License Server 激活 IDEA-Jrebel-在線-離線-均適用,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12SpringBoot使用攔截器Interceptor實(shí)現(xiàn)統(tǒng)一角色權(quán)限校驗(yàn)
角色權(quán)限校驗(yàn),是保證接口安全必備的能力:有權(quán)限才可以操作,所以,一般對(duì)于這種通用邏輯,推薦不與主業(yè)務(wù)邏輯耦合,那么怎么來(lái)解耦,那么本文小編就給大家詳細(xì)講解如何使用攔截器Interceptor實(shí)現(xiàn)統(tǒng)一角色權(quán)限校驗(yàn),需要的朋友可以參考下2023-07-07解決Weblogic部署war找不到spring配置文件的問(wèn)題
這篇文章主要介紹了解決Weblogic部署war找不到spring配置文件的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。2021-07-07java實(shí)現(xiàn)獲取安卓設(shè)備里已安裝的軟件包
本文給大家介紹的是如何獲取設(shè)備中已經(jīng)安裝的應(yīng)用軟件包的代碼,其核心方法原理很簡(jiǎn)單,我們通過(guò)Android中提供的PackageManager類,來(lái)獲取手機(jī)中安裝的應(yīng)用程序信息2015-10-10Java SpringBoot微服務(wù)框架驗(yàn)證碼報(bào)錯(cuò)問(wèn)題解決方案
這篇文章主要介紹了Java SpringBoot微服務(wù)框架驗(yàn)證碼報(bào)錯(cuò)問(wèn)題解決方案,包括dockerfile容器操作和完整dockerfile,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下2024-08-08java多線程Synchronized實(shí)現(xiàn)可見(jiàn)性原理解析
這篇文章主要介紹了java多線程Synchronized實(shí)現(xiàn)可見(jiàn)性原理,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-12-12