Package com.itjh.javaUtil;
Import java.sql.Connection;
Import Java.sql.DriverManager;
Import java.sql.PreparedStatement;
Import Java.sql.ResultSet;
Import Java.sql.ResultSetMetaData;
Import java.sql.SQLException;
Import java.util.ArrayList;
Import java.util.Collections;
Import Java.util.HashMap;
Import java.util.List;
Import Java.util.Map;
Import Org.apache.commons.dbcp.ConnectionFactory;
Import Org.apache.commons.dbcp.DriverManagerConnectionFactory;
Import Org.apache.commons.dbcp.PoolableConnectionFactory;
Import Org.apache.commons.dbcp.PoolingDriver;
Import Org.apache.commons.dbutils.DbUtils;
Import Org.apache.commons.dbutils.QueryRunner;
Import Org.apache.commons.dbutils.handlers.MapListHandler;
Import Org.apache.commons.pool.ObjectPool;
Import Org.apache.commons.pool.impl.GenericObjectPool; /** * Connect the database to a comprehensive class.
</br> * Dependent Jar Pack: commons.dbcp-1.4,commons.dbutils-1.3,commons.pool-1.5.4 package. * * @author Song Lijun * @date July 03, 2014 * * public class Dbutil {private String dri = nulL
Private String URL = null;
Private String username = null;
Private String password = null; Private String poolname = null; Connection pool name private Objectpool ConnectionPool = null;
Connection pool//corresponding timed query class private querythread querythread = null;
/** * Function: Constructor * * @author Song Lijun * @date July 03, 2014 * @param dri * Drive a full class name, for example: Com.mysql.jdbc.Driver. * @param URL * Database URL connection, for example: * "Jdbc:mysql://127.0.0.1:3306/test?autoreconnect=true&useunicode=true&c Haracterencoding=utf-8 "* @param userName * Database user name, for example: root * @param password * Database password, for example: ABC * @param p
Oolname * The name of the database connection pool created, such as mypool, note that a Web container cannot duplicate this name.
*/Public Dbutil (string dri, string URL, string userName, string password, string poolname) {This.dri = DRI;
This.url = URL;
This.username = Username;
This.password = password;
This.poolname = poolname;
/** * Executes SQL. * * @param conn * connection * @param pstm * preparedstatement * @return int Execute SQLThe corresponding impact line. * @throws SQLException * @author Song Lijun * @date July 03, 2014/public int execute (Connection conn, PreparedStatement P
STM) throws SQLException {try {return pstm.executeupdate ();
Finally {close (conn);
}/** * Query sql. * * @param conn * @param pstm * PreparedStatement * @return list<map<string,object>> Result set of query * @throws SQLException * @author Song Lijun * @date July 03, 2014/public list<map<string, object>> Query (Connection conn, PreparedStatement pstm) throws SQLException {try {return resultsettolist (Pstm.executequer
Y ());
Finally {close (conn); }/** * Features: ResultSet to list<map<string,object>> * * * @param RS * ResultSet raw data set * @r Eturn list<map<string,object>> * @throws java.sql.SQLException * @author Song Lijun * @date July 03, 2014/P Rivate list<map<string, object>> resultsettolist (ResultSet rs) throws java.sql.SQLException {if (rs = = null) return collections.empty_list; ResultSetMetaData MD = Rs.getmetadata (); The structure information of the result set (RS) is obtained, such as the number of fields, the field name and so on int columnCount = Md.getcolumncount ();
Returns the number of columns in this ResultSet object list<map<string, object>> List = new arraylist<map<string, object>> ();
map<string, object> rowdata = new hashmap<string, object> ();
while (Rs.next ()) {rowdata = new hashmap<string, object> (ColumnCount);
for (int i = 1; I <= columnCount i++) {rowdata.put (Md.getcolumnname (i), rs.getobject (i));
} list.add (RowData);
} return list;
/** * Query SQL statements. * @param SQL * Executed SQL statement * @return list<map<string,object>> * @throws SQLException * @author Song Lijun * @date July 03, 2014/Public list<map<string, object>> query (String sql) throws SQLException {Li
St<map<string, object>> results = null;
Connection conn = null;
try {conn = getconnection (); Queryrunner qr = new Queryrunner ();
Results = qr.query (conn, SQL, New Maplisthandler ());
Finally {close (conn);
return results; /** * Query SQL statements based on parameters * * @param SQL * SQL statement * @param param * parameter * @return list<map<string, object>> * @throws SQLException * @author Song Lijun * @date July 03, 2014/public list<map<string, OBJECT&G
t;> query (String sql, Object param) throws SQLException {list<map<string, object>> results = null;
Connection conn = null;
try {conn = getconnection ();
Queryrunner qr = new Queryrunner ();
Results = (list<map<string, object>>) qr.query (conn, SQL, param, New Maplisthandler ());
catch (SQLException e) {e.printstacktrace ();
Finally {close (conn);
return results; /** * Execute SQL statement * @param SQL * Executed SQL statement * @return affected rows * @throws Exception * @author Song Lijun * @da TE July 03, 2014 */public int execute (String sql) throws Exception {Connection conn = getconnection ();
int rows = 0;
try {queryrunner qr = new Queryrunner ();
rows = qr.update (conn, SQL);
Finally {close (conn);
} return rows; /** * Execute SQL statement with parameters * * @param SQL * Executed SQL statement * @param params * parameter * @return returns the affected row * @thro WS Exception * @author Song Lijun * @date July 03, 2014/public int execute (String sql, object[] params) throws Exception
{Connection conn = getconnection ();
int rows = 0;
try {queryrunner qr = new Queryrunner ();
rows = qr.update (conn, SQL, params);
Finally {close (conn);
} return rows; /** * Close Connection * * @param conn * @throws SQLException * @author Song Lijun * @date July 03, 2014/public void Clo
SE (Connection conn) throws SQLException {if (conn!= null) {conn.close ();
} dbutils.closequietly (conn); /** * Start Connection Pool * * @author Song Lijun * @date July 03, 2014/private void Startpool () {try {class.forname (dri
); } catch (ClassNotFoundException E1) {e1.printstacktrace ();
} if (ConnectionPool!= null) {Shutdownpool ();
try {connectionpool = new Genericobjectpool (null);
ConnectionFactory connectionfactory = new drivermanagerconnectionfactory (URL, username, password); Poolableconnectionfactory poolableconnectionfactory = new Poolableconnectionfactory (connectionFactory, ConnectionPo
OL, NULL, "SELECT 1", false, true);
Class.forName ("Org.apache.commons.dbcp.PoolingDriver");
Poolingdriver Driver = (poolingdriver) drivermanager. Getdriver ("JDBC:APACHE:COMMONS:DBCP:");
Driver.registerpool (poolname, Poolableconnectionfactory.getpool ());
catch (Exception e) {e.printstacktrace ();
//Open the query program Querythread = new Querythread (this);
Querythread.start (); /** * Close Connection Pool * * @author Song Lijun * @date July 03, 2014/private void Shutdownpool () {try {Poolingdriver Driver = (poolingdriver) drivermanager. Getdriver ("Jdbc:apache:commoNS:DBCP: ");
Driver.closepool (poolname);
Turn off the timed query Querythread.setstartquery (FALSE);
catch (SQLException e) {e.printstacktrace (); }/** * Get a connection * * @return * @author Song Lijun * @date July 03, 2014/Public synchronized Connection Getconne
Ction () {Connection conn = null;
try {if (ConnectionPool = null) Startpool ();
conn = Drivermanager.getconnection ("jdbc:apache:commons:dbcp:" + poolname);
catch (Exception e) {e.printstacktrace ();
Return conn;
}/** * When the connection pool is started, the database is automatically timed to prevent the database connection from timing out.
* * @author Song Lijun * @date July 03, 2014/class Querythread extends Thread {private Dbutil dbutil = null;
Whether to open query private Boolean startquery = true;
/** * Function: The corresponding database connection. * * @author Song Lijun * @date July 03, 2014 * @param dbutil * Database connection/public querythread (Dbutil dbutil) {This
. Dbutil = Dbutil;
public void Run () {while (true) {try {if (startquery) {this.dbUtil.query ("Select 1"); }
//System.out.println (startquery+ "123");
catch (Exception e) {e.printstacktrace ();
Finally {try {thread.sleep (120000);
catch (Interruptedexception e) {e.printstacktrace ();
}}} public void Setstartquery (Boolean startquery) {//System.out.println ("Startquery shut:" +startquery);
This.startquery = Startquery; }
}