A packaged tool class that links Oracle databases to easily get connection objects to close statement, ResultSet, statment objects, and so on
Copy Code code as follows:
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;
/**
* Link Oracle Database
* @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 ("Failed to load Oracle database driver!") ");
}
}
/**
* Get 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 ("Get database connection failed!") ");
Throw e;
}
Return conn;
}
/**
* Close ResultSet
* @param RS
*/
public static void Closeresultset (ResultSet rs) {
if (Rs!= null) {
try {
Rs.close ();
catch (SQLException e) {
System.out.println (E.getmessage ());
}
}
}
/**
* Close Statement
* @param stmt
*/
public static void Closestatement (Statement stmt) {
if (stmt!= null) {
try {
Stmt.close ();
}
catch (Exception e) {
System.out.println (E.getmessage ());
}
}
}
/**
* Close ResultSet, Statement
* @param RS
* @param stmt
*/
public static void Closestatement (ResultSet rs, Statement stmt) {
Closeresultset (RS);
Closestatement (stmt);
}
/**
* Close PreparedStatement
* @param pstmt
* @throws SQLException
*/
public static void Fastclosestmt (PreparedStatement pstmt) throws SQLException
{
Pstmt.close ();
}
/**
* Close ResultSet, PreparedStatement
* @param RS
* @param pstmt
* @throws SQLException
*/
public static void Fastclosestmt (ResultSet rs, PreparedStatement pstmt) throws SQLException
{
Rs.close ();
Pstmt.close ();
}
/**
* 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);
}
/**
* Close statement, Connection
* @param stmt
* @param con
*/
public static void CloseConnection (Statement stmt, Connection con) {
Closestatement (stmt);
CloseConnection (con);
}
/**
* Close Connection
* @param con
*/
public static void CloseConnection (Connection con) {
if (con!= null) {
try {
Con.close ();
}
catch (Exception e) {
System.out.println (E.getmessage ());
}
}
}
}
The above is the entire content described in this article, I hope that the small partners can enjoy.