java連接Oracle數(shù)據(jù)庫的工具類
一個(gè)封裝好的鏈接Oracle數(shù)據(jù)庫的工具類,可以方便的獲取Connection對(duì)象關(guān)閉Statement、ResultSet、Statment對(duì)象等等
package myUtil;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
* 鏈接oracle數(shù)據(jù)庫
* @author weichk
*/
public class OracleDbManager {
private static final String URL = "jdbc:oracle:thin:@//localhost:1521/databaseName";
private static final String USER = "username";
private static final String PASSWORD = "password";
static {
try {
Class.forName("oracle.jdbc.OracleDriver");
} catch (ClassNotFoundException e) {
System.out.println("加載Oracle數(shù)據(jù)庫驅(qū)動(dòng)失??!");
}
}
/**
* 獲取Connection
*
* @return
* @throws SQLException
* @throws ClassNotFoundException
*/
public static Connection getConnection() throws SQLException {
Connection conn = null;
try {
conn = DriverManager.getConnection(URL, USER, PASSWORD);
} catch (SQLException e) {
System.out.println("獲取數(shù)據(jù)庫連接失??!");
throw e;
}
return conn;
}
/**
* 關(guān)閉ResultSet
* @param rs
*/
public static void closeResultSet(ResultSet rs) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
}
/**
* 關(guān)閉Statement
* @param stmt
*/
public static void closeStatement(Statement stmt) {
if (stmt != null) {
try {
stmt.close();
}
catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
/**
* 關(guān)閉ResultSet、Statement
* @param rs
* @param stmt
*/
public static void closeStatement(ResultSet rs, Statement stmt) {
closeResultSet(rs);
closeStatement(stmt);
}
/**
* 關(guān)閉PreparedStatement
* @param pstmt
* @throws SQLException
*/
public static void fastcloseStmt(PreparedStatement pstmt) throws SQLException
{
pstmt.close();
}
/**
* 關(guān)閉ResultSet、PreparedStatement
* @param rs
* @param pstmt
* @throws SQLException
*/
public static void fastcloseStmt(ResultSet rs, PreparedStatement pstmt) throws SQLException
{
rs.close();
pstmt.close();
}
/**
* 關(guān)閉ResultSet、Statement、Connection
* @param rs
* @param stmt
* @param con
*/
public static void closeConnection(ResultSet rs, Statement stmt, Connection con) {
closeResultSet(rs);
closeStatement(stmt);
closeConnection(con);
}
/**
* 關(guān)閉Statement、Connection
* @param stmt
* @param con
*/
public static void closeConnection(Statement stmt, Connection con) {
closeStatement(stmt);
closeConnection(con);
}
/**
* 關(guān)閉Connection
* @param con
*/
public static void closeConnection(Connection con) {
if (con != null) {
try {
con.close();
}
catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
}
以上就是本文所述的全部內(nèi)容了,希望小伙伴們能夠喜歡。
相關(guān)文章
Spring Boot集成Quartz注入Spring管理的類的方法
本篇文章主要介紹了Spring Boot集成Quartz注入Spring管理的類的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-04-04
java后臺(tái)利用Apache poi 生成excel文檔提供前臺(tái)下載示例
本篇文章主要介紹了java后臺(tái)利用Apache poi 生成excel文檔提供前臺(tái)下載示例,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-05-05
Hibernate的Session_flush與隔離級(jí)別代碼詳解
這篇文章主要介紹了Hibernate的Session_flush與隔離級(jí)別代碼詳解,分享了相關(guān)代碼示例,小編覺得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-02-02
Springboot實(shí)現(xiàn)XSS漏洞過濾的示例代碼
這篇文章主要介紹了Springboot實(shí)現(xiàn)XSS漏洞過濾的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
淺談對(duì)象數(shù)組或list排序及Collections排序原理
下面小編就為大家?guī)硪黄獪\談對(duì)象數(shù)組或list排序及Collections排序原理。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-09-09
MyBatis多對(duì)多關(guān)聯(lián)映射創(chuàng)建示例
這篇文章主要為大家介紹了MyBatis多對(duì)多關(guān)聯(lián)映射的創(chuàng)建示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06

