Example analysis of encapsulating JDBC tool class in java
- 2021-08-31 08:14:17
- OfStack
For code that can be reused, our best approach is to encapsulate it, and then call it directly in the next use. What this article will mention is the JDBC tool class, which I believe everyone has come into contact with when learning java. Then for the method of encapsulating it, this article first gives a simple description of the tool class, lists the relevant encapsulation steps, and then brings relevant examples.
1. Description
In the java development process, the code commonly used to some Scanner, Random1-like classes, they are keyboard input, generate random numbers of classes, like a tool 1, in java is called tool class.
2. Steps
Encapsulate the JDBC tool class
Add a method to get the database connection object
Add a method to release the connection
3. Examples
package com.qianfeng.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
* JDBC Tool class
* There is a method to get the connection
* @author dushine
*/
public class JDBCUtil {
/**
* Method for getting database connection
* @return Connection conn
* @throws SQLException
*/
public static Connection getConnection() throws SQLException {
String url = "jdbc:mysql://localhost:3306/class?useSSL=false";
String user = "root";
String password = "root";
Connection conn = DriverManager.getConnection(url,user,password);
return conn;
}
/**
* Method of releasing connection
* @param conn
* @throws SQLException
*/
public static void releaseSourse(Connection conn) throws SQLException {
if (conn != null) {
conn.close();
}
}
/**
* Method of releasing connection
* @param conn Database connection object
* @param stmt Execute SQL Object of the statement
* @throws SQLException
*/
public static void releaseSourse(Connection conn,Statement stmt) throws SQLException {
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
}
/**
* Method of releasing connection
* @param conn Database connection object
* @param stmt Execute SQL Object of the statement
* @param resultSet Execute SQL The result set returned by the statement
* @throws SQLException
*/
public static void releaseSourse(Connection conn,Statement stmt,ResultSet resultSet) throws SQLException {
if (resultSet != null) {
resultSet.close();
}
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
}
}
Instance extension:
public class JDBCUtil {
// Connection object
private Connection connection = null;
// Database operation object
private PreparedStatement ps = null;
// Database connection address
private static String url = "jdbc:mysql://localhost:3306/";
// User name
private static String user = "root";
// Password
private static String password = "123456";
// Static code block Register driver
// Class is loaded, only the execution of the 1 Times
static{
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
// Get the connection object
public Connection getConnection(){
//Connection conn = null;
try {
connection = DriverManager.getConnection(url,user,password);
} catch (SQLException e) {
e.printStackTrace();
System.out.println(" Database connection failed ....");
}
System.out.println(" Database connection succeeded ...");
return connection;
}
// Get the database operation object
public PreparedStatement createPreparedStatement(String sql){
connection = getConnection();
try {
ps = connection.prepareStatement(sql);
} catch (SQLException e) {
e.printStackTrace();
}
return ps;
}
// Release resources
public void close(){
// Release the connection object
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
// Release database operation objects
if (ps != null) {
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
System.out.println(" Resource released successfully ...");
}
// Overload of method
public void close(ResultSet reuslt){
// Call the method that releases the resource
close();
// Release the query result set object
if (reuslt != null) {
try {
reuslt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}