A packaged and linked MySQL database tool class, you can easily get connection objects 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 MySQL Database
* @author Weichk
*/
public class Mysqldbmanager {
private static final String URL = "Jdbc:mysql://127.0.0.1:3306/openfire";
private static final String USER = "root";
private static final String PASSWORD = "123456";
static {
try {
Class.forName ("Com.mysql.jdbc.Driver");
catch (ClassNotFoundException e) {
System.out.println ("Loading MySQL database driver failed!") ");
}
}
/**
* 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 of this article, I hope that you can have a good command of Java Help.