java連接Oracle數(shù)據(jù)庫的工具類
一個(gè)封裝好的鏈接Oracle數(shù)據(jù)庫的工具類,可以方便的獲取Connection對象關(guān)閉Statement、ResultSet、Statment對象等等
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)文章
SpringBoot 使用jwt進(jìn)行身份驗(yàn)證的方法示例
這篇文章主要介紹了SpringBoot 使用jwt進(jìn)行身份驗(yàn)證的方法示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-12-12
Java計(jì)算代碼段執(zhí)行時(shí)間的詳細(xì)過程
java里計(jì)算代碼段執(zhí)行時(shí)間可以有兩種方法,一種是毫秒級別的計(jì)算,另一種是更精確的納秒級別的計(jì)算,這篇文章主要介紹了java計(jì)算代碼段執(zhí)行時(shí)間,需要的朋友可以參考下2023-02-02
Java使用HttpClient實(shí)現(xiàn)Post請求實(shí)例
本篇文章主要介紹了Java使用HttpClient實(shí)現(xiàn)Post請求實(shí)例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-02-02
Java通過wait()和notifyAll()方法實(shí)現(xiàn)線程間通信
這篇文章主要為大家詳細(xì)介紹了Java通過wait()和notifyAll()方法實(shí)現(xiàn)線程間通信的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04
Java的非阻塞隊(duì)列ConcurrentLinkedQueue解讀
這篇文章主要介紹了Java的非阻塞隊(duì)列ConcurrentLinkedQueue解讀,在并發(fā)編程中,有時(shí)候需要使用線程安全的隊(duì)列,如果要實(shí)現(xiàn)一個(gè)線程安全的隊(duì)列有兩種方式:一種是使用阻塞算法,另一種是使用非阻塞算法,需要的朋友可以參考下2023-12-12
Mybatis動(dòng)態(tài)sql中@Param使用詳解
這篇文章主要介紹了Mybatis動(dòng)態(tài)sql中@Param使用詳解,當(dāng)方法的參數(shù)為非自定義pojo類型,且使用了動(dòng)態(tài)sql,那么就需要在參數(shù)前加上@Param注解,需要的朋友可以參考下2023-10-10
Java之JFrame輸出Helloworld實(shí)例
這篇文章主要介紹了Java之JFrame輸出Helloworld的方法,以輸出Helloworld的實(shí)例分析了JFrame的簡單入門技巧,需要的朋友可以參考下2015-02-02

