Java Common Tools Class (III)

Source: Internet
Author: User

One, connect the database the synthesis class

[Java] View Plain copy print? 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; &nbsp import org.apache.commons.dbutils.handlers.maplisthandler;   import  org.apache.commons.pool.objectpool;   import org.apache.commons.pool.impl.genericobjectpool;      /**   *  connection to the integrated database class. </br>   *  Dependent Jar Pack: commons.dbcp-1.4,commons.dbutils-1.3,commons.pool-1.5.4 package.    *    *  @author   Song Lijun    *  @date  2014 year July 03    */       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 &NBSP;&NBsp     private ObjectPool connectionPool = null; //  Connection Pool         //  corresponding timing query class        private querythread  queryThread = null;          /**        *  function: constructor        *        *   @author   Song Lijun        *  @date  2014 year July 03         *  @param  dri       *              drive a full class name, for example: Com.mysql.jdbc.Driver.        *  @param  url       *              database URL connection, such as:       *              "jdbc:mysql://127.0.0.1:3306/test?autoreconnect=true&useunicode=true& Characterencoding=utf-8 "       *  @param  userName        *             database user name, for example: root        *  @param  password       *              database passwords, such as:abc        *  @param  poolName       *              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;       }           /**       *  Execute SQL.        *        *  @param  conn       *             Connection        *  @param  pstm       *             preparedstatement       *  @return  int  Execute SQL corresponding impact line.        *  @throws  SQLException       *   @author   Song Lijun        *  @date  2014 year July 03         */       public int execute (Connection conn,  preparedstatement pstm)                 throws SQLException {           try  {               return  Pstm.executeupdate ();           } finally {    &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;CLOSE (conn);            } &nBsp     }          /**        *  query SQL.        *        *  @param  conn       *             Connection        *  @param  pstm       *             PreparedStatement        *  @return  List<Map<String,Object>>  Query result set         *  @throws  SQLException       *  @author   Song Lijun         *  @date  2014 Year July 03        */        public list<map<string, object>> query (Connection conn,               preparedstatement pstm )  throws SQLException {           try {                return  Resultsettolist (Pstm.executequery ());           }  finally {               close (conn);            }       }          /**       *  function:resultset  to list< map<string,object>>       *        *         *  @param  rs       *             ResultSet  raw Data Set        * @ return list<map<string,object>>       *  @throws   java.sql.sqlexception       *  @author   Song Lijun         *  @date  2014 Year July 03        */        Private list<map<string, object>> resultsettolist (RESULTSET&NBSP;RS)                throws java.sql.sqlexception  {           if  (rs == null)                return Collections.EMPTY_LIST;               ResultSetMetaData md =  Rs.getmetadata ();  //  Gets the structure information of the result set (RS), such as field number, field name            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++)  {    &NBSP;&NBSP;&NBSP;&NBSP;            rowdata.put (Md.getColumnName (i),  rs.getobject (i));               }                list.add (rowData);            }            return list;       }          /* *       *  Query SQL statements.        *        *  @param  sql       *             Executed SQL statements        *  @return  List<Map<String,Object>>       *  @throws  SQLException  &NBSP;&Nbsp;   *  @author   Song Lijun        *  @date  2014 Year July 03        */       public list<map<string,  object>> query (string sql)  throws SQLException {            List<Map<String, Object>> results = null;            Connection conn = null;           try {                conn = getconnection ();                queryrunner qr = new queryrunner ();                results = qr.query ( Conn, sql,&nbSp;new maplisthandler ());           } finally  {               close (conn);           }            return results;       }          /**        *  Query SQL statements based on parameters        *         *  @param  sql       *             sql statement        *  @param   param       *              Parameters        *  @return  List<Map<String,Object>>       *  @throws  SQLException       *  @author   Song Lijun        *  @date  2014 year July 03        */        public list<map<string, object>> 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;       }      &NBSP;&NBSP;&Nbsp; /**       *  Execute SQL statements        *         *  @param  sql       *              executed SQL statements        *   @return   Affected rows        *  @throws  Exception        *  @author   Song Lijun        *  @date  2014 Year July 03        */       public int execute (String  sql)  throws Exception {            Connection conn = getconnection ();            int rows = 0;           try {      &nbsP;        queryrunner qr = new queryrunner ();                rows = qr.update (conn, sql);           } finally {               close (conn);            }           return  rows;       }  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.