Java Connection to Oracle database tool class _java

Source: Internet
Author: User
Tags getmessage stmt

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.