This is a relatively high code quality util. Here are two types, one primitive and one using C3P0. The database is using MySQL5.7.
There is no need to write these things when using framework development. In fact, the framework is encapsulated in this way, but the functionality provided will be very rich. Here is the idea, for reference.
Full-featured DAO tool class
DriverManager get the Util of the connection:
package util;import java.io.ioexception;import java.sql.connection;import java.sql.drivermanager;import java.sql.resultset;import java.sql.sqlexception;import java.sql.statement;import java.util.properties;public class dbutil {//Connection urlprivate static string jdbcurl;//logged in user, the configuration file key is not named Username or username, there may be conflict situations private static string username;//Login Password private static string password;//Instantiate the local thread object private static Threadlocal<connection> thread = new threadlocal<connection> (); static{try {//gets the JDBC database connection information in the configuration file Properties props = new properties ();p rops.load ( DBUtil.class.getResourceAsStream ("/conf/dbinfo.properties")); String driverclass = props.getproperty ("Driverclass"); Jdbcurl = props.getproperty (" Jdbcurl "), Username = props.getproperty (" user ");p assword = props.getproperty (" password ");// Load Driver CLass.forname (Driverclass);} catch (classnotfoundexception e) {e.printstacktrace ();} catch (ioexception e) {e.printstacktrace ();}} /** * Get database connections * @return Database Connections connection Objects * @throws SQLException */public static connection getconnection () throws sqlexception{if (Thread.get () = = null) {connection conn = drivermanager.getconnection (jdbcurl, username, password); Thread.set (conn);} Return thread.get ()///Do not need to use local thread, you can remove the above code//return drivermanager.getconnection (jdbcurl, username , password);} /** * Close the connection, release resources * @param conn connection objects * @param stmt Statement object that can receive subclasses PreparedStatement objects * @param rs resultset objects */public static void closeall (Connection conn,statement stmt,resultset rs) {if (rs != null) { Try {rs.close ();} catch (sqlexception e) {e.printstacktrace ();}} if (stmt != null) {try {stmt.close ();} catch (sqlexception e) {e.printstacktrace ();}} if (conn != null) {try {conn.close ();} catch (sqlexception e) {e.printstacktrace ();}} Thread.remove ();//Remove Connection object from local thread}public static void main (String[] args) throws sqlexception {system.out.println (getconnection ());}}
using the C3P0 connection pool util:
package com.sourong.util;import java.beans.propertyvetoexception;import java.io.ioexception; import java.sql.connection;import java.sql.resultset;import java.sql.sqlexception;import java.sql.statement;import java.util.properties;import com.mchange.v2.c3p0.combopooleddatasource;/** &NBSP;*&NBSP;C3P0 Connection Pooling Tool class * */public class c3p0dbutil {//instantiate C3P0 Connection pool object private Static combopooleddatasource c3p0 = new combopooleddatasource ();//Instantiate local thread object private static ThreadLocal<Connection> thread = new ThreadLocal<Connection> (); static{//to c3p0 configuration Parameters try {properties props = new properties ();p rops.load ( C3p0DBUtil.class.getResourceAsStream ("/datasource.properties"));//Set database Connection driver class C3p0.setdriverclass ( Props.getproperty ("Driverclass"));//Set JDBC Connection Urlc3p0.setjdbcurl (Props.getproperty ("Jdbcurl"));// Set the connection login username C3p0.setuser (props.getproperty ("user"));//Set the connection login password c3p0.SetPassword (Props.getproperty ("password"));//sets the number of connections that are fetched at the same time c3p0 the connection pool is exhausted c3p0.setacquireincrement ( Integer.valueof (Props.getproperty ("Acquireincrement")));//set to get 10 connections when initializing, The value should be between Minpoolsize and Maxpoolsize c3p0.setinitialpoolsize (integer.valueof (Props.getproperty ("initialPoolSize"));// Set the minimum number of connections retained in the connection pool C3p0.setminpoolsize (integer.valueof (Props.getproperty ("minpoolsize"));// Set the maximum number of connections retained in the connection pool C3p0.setmaxpoolsize (integer.valueof (Props.getproperty ("maxpoolsize"));} catch (propertyvetoexception e) {e.printstacktrace ();} catch (ioexception e) {e.printstacktrace ();}} /** * C3P0 Connection pool for database connections * @return Database Connections connection Objects * @throws Sqlexception */public static connection getconnection () throws sqlexception{if ( Thread.get () == null) {connection conn = c3p0.getconnection (); Thread.set (conn);} Return thread.get ();} /** * close connection, release resources * @param conn conneCtion Object * @param stmt statement object, can receive subclasses PreparedStatement objects * @param rs resultset Object */public static void closeall (connection conn,statement stmt, RESULTSET&NBSP;RS) {if (rs != null) {try {rs.close ();} catch (sqlexception e) {e.printstacktrace ();}} if (stmt != null) {try {stmt.close ();} catch (sqlexception e) {e.printstacktrace ();}} if (conn != null) {try {conn.close ();//Place the connection object into the connection pool and mark it as Idle} catch (sqlexception e) {e.printstacktrace ();}} Thread.remove ();//Remove Connection object from local thread}public static void main (String[] args) throws sqlexception {system.out.println (getconnection ());}}
Custom Database Connection Tool classes