In Java programs, the general use of JDBC to connect to the database, more trouble, in the wisdom of the tutorial learned a tool class, to simplify the operation between the database and the steps, that is, Txqueryrunner, he is the queryrunner of the sub-class, used and he is the same, characterized by support affairs, You need to import several jar packages when you are using them, respectively
:
The advantages of this tool class is basically the Apache out of the Dbutils framework inside the features, greatly simplifying the code of the operator, the underlying use of C3P0 connection pool, you can easily database query results mapped to JAVABEAN,LIST,MAP, etc., the following are notes and code:
Importjava.sql.SQLException;Importjava.util.List;ImportJava.util.Map;ImportOrg.apache.commons.dbutils.QueryRunner;ImportOrg.apache.commons.dbutils.handlers.BeanHandler;ImportOrg.apache.commons.dbutils.handlers.BeanListHandler;ImportOrg.apache.commons.dbutils.handlers.MapHandler;ImportOrg.apache.commons.dbutils.handlers.MapListHandler;ImportOrg.apache.commons.dbutils.handlers.ScalarHandler;Importorg.junit.Test;Importcn.itcast.commons.CommonUtils;Importcn.itcast.jdbc.JdbcUtils;ImportCn.itcast.jdbc.TxQueryRunner;/*** Txqueryrunner It is a subclass of Queryrunner! (Commons-dbutils.jar) * Available up to queryrunner similar! * Our class supports the business! It uses jdbcutils at the bottom to get the connection! * * Simplified JDBC Operation * Queryrunner three methods: * UPDATE ()--INSERT, UPDATE, delete * query ()--Select * * Batch ()-- Batch processing *@authorQdmmy6 **/ Public classTxqueryrunnertest {/*** Test Update () method to execute INSERT, UPDATE, DELETE statement *@throwsSQLException*/@Test Public voidTestupdate ()throwsSQLException {String SQL= "INSERT into T_person (pid,pname,age,sex) VALUES (?,?,?,?)"; Object[] Params= {"1", "P1", 1, "Male"};//to the corresponding parameter in SQLqueryrunner qr=NewTxqueryrunner ();//we did not provide a connection pool to the objectQr.update (sql, params);//executes SQL and does not provide a connection, which internally uses jdbcutils to get the connection } /*** Use transaction *@throwsSQLException*/@Test Public voidTestUpdate2 ()throwsException {Try{jdbcutils.begintransaction ();//Open TransactionString SQL= "INSERT into T_person (pid,pname,age,sex) VALUES (?,?,?,?)"; Queryrunner qr=NewTxqueryrunner (); Object[] Params= {"2", "P2", 2, "female"}; Qr.update (sql, params);//Execution if(false) { Throw NewException (); } params=Newobject[]{"3", "P3", 3, "female"}; Qr.update (sql, params);//Executionjdbcutils.committransaction ();//Commit a transaction}Catch(Exception e) {Try{jdbcutils.rollbacktransaction ();//rolling back a transaction}Catch(SQLException E1) {}Throwe; } } /*** Test Query method * We know that the result of the JDBC query is resultset * and the result of the Queryrunner query is the data mapped by resultset. * * Queryrunner The first step is to perform SELECT, Get resultset * * Convert resultset to other types! * By converting results: * * JavaBean: Encapsulate result set into JavaBean * Map: Encapsulate result set into map * * Enclose result set in object (result set is single row) * @throwsSQLException * **/ /** Single-row result sets are mapped to JavaBean*/@Test Public voidTestQuery1 ()throwsSQLException {String SQL= "SELECT * from T_person where pid=?"; Queryrunner qr=NewTxqueryrunner (); /** The second parameter type is Resultsethandler, which is an interface that represents the result type of the mapping. * * Beanhandler----It is the implementation class of Resultsethandler, which is the function of encapsulating the result set into the person object*/Person P= Qr.query (SQL,NewBeanhandler<person> (person.class), "1"); SYSTEM.OUT.PRINTLN (P); } /*** Use Beanlisthandler * to map multi-line result sets to List<bean>, that is, multiple JavaBean objects. * One row of result set records corresponds to a JavaBean object, and multiple lines correspond to list<bean> *@throwsSQLException*/@Test Public voidTestQuery2 ()throwsSQLException {String SQL= "SELECT * FROM T_person"; Queryrunner qr=NewTxqueryrunner (); /** The second parameter type is Resultsethandler, which is an interface that represents the result type of the mapping. * * Beanlisthandler--It is the implementation class of Resultsethandler, * it is to encapsulate the result set into the List<person> object*/List<Person> list = qr.query (sql,NewBeanlisthandler<person> (person.class)); SYSTEM.OUT.PRINTLN (list); } /*** Use Maphandler to encapsulate a single-row result set into a map object *@throwsSQLException*/@Test Public voidTestQuery3 ()throwsSQLException {String SQL= "SELECT * from T_person where pid=?"; Queryrunner qr=NewTxqueryrunner (); /** The second parameter type is Resultsethandler, which is an interface that represents the result type of the mapping. * * Beanlisthandler--It is the implementation class of Resultsethandler, * it is to encapsulate the result set into the List<person> object*/Map<string, object> map = qr.query (sql,NewMaphandler (), "1"); SYSTEM.OUT.PRINTLN (map); } /*** Use Maplisthandler to encapsulate multi-line result set into list<map>, that is, multiple map * lines correspond to a map, multi-line corresponding list<map> *@throwsSQLException*/@Test Public voidTestQuery4 ()throwsSQLException {String SQL= "SELECT * FROM T_person"; Queryrunner qr=NewTxqueryrunner (); /** The second parameter type is Resultsethandler, which is an interface that represents the result type of the mapping. * * Beanlisthandler--It is the implementation class of Resultsethandler, * it is to encapsulate the result set into the List<person> object*/List<map<string, object>> maplist = qr.query (sql,NewMaplisthandler ()); System.out.println (maplist); } /*** Use Scalarhandler to encapsulate a single-column result set in Object *@throwsSQLException*/@Test Public voidTestQuery5 ()throwsSQLException {String SQL= "SELECT count (*) from T_person";//The result set is a single-rowQueryrunner QR =NewTxqueryrunner (); Object obj= Qr.query (SQL,NewScalarhandler ()); /** We know that select COUNT (1), the result must be an integer! * > Integer * > Long * > BigInteger * * Different drivers, different results! * Regardless of the type, it is number type! Strong to number must not go wrong*/ Number Number=(number) obj; LongCNT =Number.longvalue (); SYSTEM.OUT.PRINTLN (CNT); } /*** A row of result sets contains two table columns * Use Maphandler to process * 1. Encapsulate the result set in Map * 2. Use map to generate a person object * 3. Use map to generate address object * 4. Establish relationships with two entity objects *@throwsSQLException*/@Test Public voidTestQuery6 ()throwsSQLException {String SQL= "SELECT * from T_person p, t_address a WHERE p.aid=a.aid and p.pid=?"; Queryrunner qr=NewTxqueryrunner (); /** 1. Get map*/map Map= Qr.query (SQL,NewMaphandler (), "AAA"); /** 2. Encapsulate some of the data in the map into the person*/Person P= Commonutils.tobean (map, person.class); /** 3. Encapsulate some of the data in the map into address*/Address Addr= Commonutils.tobean (map, Address.class); /** 4. Establish relationships with two entities*/p.setaddress (addr); SYSTEM.OUT.PRINTLN (P); }}
Java Operational Database additions and deletions to the gadget--txqueryrunner