java銜接Oracle數據庫的對象類。本站提示廣大學習愛好者:(java銜接Oracle數據庫的對象類)文章只能為提供參考,不一定能成為您想要的結果。以下是java銜接Oracle數據庫的對象類正文
一個封裝好的鏈接Oracle數據庫的對象類,可以便利的獲得Connection對象封閉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數據庫
* @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數據庫驅動掉敗!");
}
}
/**
* 獲得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("獲得數據庫銜接掉敗!");
throw e;
}
return conn;
}
/**
* 封閉ResultSet
* @param rs
*/
public static void closeResultSet(ResultSet rs) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
}
/**
* 封閉Statement
* @param stmt
*/
public static void closeStatement(Statement stmt) {
if (stmt != null) {
try {
stmt.close();
}
catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
/**
* 封閉ResultSet、Statement
* @param rs
* @param stmt
*/
public static void closeStatement(ResultSet rs, Statement stmt) {
closeResultSet(rs);
closeStatement(stmt);
}
/**
* 封閉PreparedStatement
* @param pstmt
* @throws SQLException
*/
public static void fastcloseStmt(PreparedStatement pstmt) throws SQLException
{
pstmt.close();
}
/**
* 封閉ResultSet、PreparedStatement
* @param rs
* @param pstmt
* @throws SQLException
*/
public static void fastcloseStmt(ResultSet rs, PreparedStatement pstmt) throws SQLException
{
rs.close();
pstmt.close();
}
/**
* 封閉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);
}
/**
* 封閉Statement、Connection
* @param stmt
* @param con
*/
public static void closeConnection(Statement stmt, Connection con) {
closeStatement(stmt);
closeConnection(con);
}
/**
* 封閉Connection
* @param con
*/
public static void closeConnection(Connection con) {
if (con != null) {
try {
con.close();
}
catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
}
以上就是本文所述的全體內容了,願望小同伴們可以或許愛好。