Java JDBC Custom Encapsulation Tool Class Steps and Complete Code

  • 2021-08-31 07:43:48
  • OfStack

Encapsulating the role of JDBC tool classes can optimize code and improve development efficiency

Steps

Create a configuration file (config. properties) to store the parameter values needed when registering the driver and connecting to the database

② Customize 1 class (JDBCUtils. java)

③ Implement the construction of private method private JDBCUtils () {}

④ Declare the required configuration variables


private static String driverClass;
private static String url;
private static String username;
private static String password;
private static Connection conn;

Providing static code quantity, reading configuration file, assigning value to configuration variable, registering driver

⑥ Define the database connection method public static Connection getConnection ();

⑦ Define the method of releasing resources (two)


//  The method of releasing resources used when querying operations 
public static void close(Connection conn, Statement st, ResultSet rs);
//  The method of releasing resources used in adding, deleting and modifying operations 
public static void close(Connection conn, Statement st)

Complete code


package com.cmy.utils;

import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

/**
 * JDBC Tool class 
 * @author ChenMingYong
 */
public class JDBCUtils {
	/**
	 * 1. Private constructor 
	 */
	private JDBCUtils(){}
	
	/**
	 * 2. Declare the required configuration variables 
	 */
	private static String driverClass;
	private static String url;
	private static String username;
	private static String password;
	private static Connection conn;
	
	// 3. Provide static code blocks, read configuration file information, assign values to variables, and register drivers 
	static {
		try{
			//  Read the information of the configuration file and assign values to variables 
			InputStream is = JDBCUtils.class.getClassLoader().getResourceAsStream("config.properties");
			Properties prop = new Properties();
			prop.load(is);
			driverClass = prop.getProperty("driverClass");
			url = prop.getProperty("url");
			username = prop.getProperty("username");
			password = prop.getProperty("password");
			//  Register driver 
			Class.forName(driverClass);
		}
		catch (Exception e){
			e.printStackTrace();
		}
	
	}
	
	/**
	 * 4. Provide database connection methods 
	 * @return
	 */
	public static Connection getConnection(){
		try {
			conn = DriverManager.getConnection(url, username, password);
		}
		catch (Exception e){
			e.printStackTrace();
		}
		return conn;
	}
	
	/**
	 * 5. The method of releasing resources used when querying operations 
	 * @param conn
	 * @param st
	 * @param rs
	 */
	public static void close(Connection conn, Statement st, ResultSet rs){
		if(conn != null){
			try{
				conn.close();
			}
			catch (SQLException e){
				e.printStackTrace();
			}
		}
		if(st != null){
			try{
				st.close();
			}
			catch (SQLException e){
				e.printStackTrace();
			}
		}
		if(rs != null){
			try{
				rs.close();
			}
			catch (SQLException e){
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 5. The method of releasing resources used in adding, deleting and modifying operations 
	 * @param conn
	 * @param st
	 */
	public static void close(Connection conn, Statement st){
		if(conn != null){
			try{
				conn.close();
			}
			catch (SQLException e){
				e.printStackTrace();
			}
		}
		if(st != null){
			try{
				st.close();
			}
			catch (SQLException e){
				e.printStackTrace();
			}
		}
	}
	
}

Summarize


Related articles: