The combination of C3P0 and Queryrunner makes development easier

Source: Internet
Author: User

Use of Queryrunner in 1:dbutils:

The API for SQL statement operations is provided in 1.1:queryrunner.

1.2: There are three main methods:

1.2.1:query (): Used to execute select (query);

1.2.2:update (): Used to perform insert (insert)/update (update)/delete (delete);

1.2.3:batch (): Batch processing;

Combined use of 2:c3p0 and Queryrunner:

2.1: First guide the package, as shown below the package;

C3p0-0.9.1.2.jar
Commons-dbutils-1.6.jar
Mysql-connector-java-5.1.12-bin.jar

2.2: Of course, you need to create a database and a data sheet before you lead the package. ~~~

Remember to configure the C3p0-config.xml file under the SRC directory

1 <c3p0-config>
 2     
 3     <!--c3p0 default configuration, you can also configure multiple databases--
 4     <default-config>
 5         <property name= "Jdbcurl" >jdbc:mysql://localhost:3306/test
 6         </property>
 7         <property name= "Driverclass" >com.mysql.jdbc.Driver</property>
 8         <property name= "User" >root</property>
 9         <property name= "password" >123456</property>         < Property Name= "Initialpoolsize" >6</property>         <property name= "Maxpoolsize" >50</ property>         <property name= "MaxIdleTime" >1000</property>     </ default-config> 
</c3p0-config>

2.3: Create entity classes, such as User.java, the source code is as follows,

 1 package Com.bie.po; 2/** 3 * @author Biehongli 4 * @version created: March 11, 2017 pm 12:55:21 5 * 6 */7 public class User {8 9 pri
vate int id;
Ten private String name;
One private String password;
The private String email;
Private String phone;     + public int getId () {return id;.)-public void setId (int id) {this.id = ID; 19         } public String GetName () {return name; \ n} The public void SetName (String name) {24
THIS.name = name; ()-Public String GetPassword () {password;}, public void SetPassword (string p     Assword) {this.password = password;} public String Getemail () {return email; 34 } public void Setemail (string email) {this.email = email; PNS} public String Getphone () {3
9 return phone; Setphone public void (String phone) {. phone = phone; () @Override The public String toString () {"User [, name=" + name + ", password=" + pas
Sword + ", email=" + email + ", phone=" + phone 47 + "]"; 48} 49 50 51}
View Code

2.4: After creating the entity class, you can develop it according to the MVC pattern layer, which is just the simulation, so the DAO and Servic layers and the test layer are created. Test layer for testing;

So first create the DAO layer to create the interface, and then create the implementation of the interface class, of course, the implementation of C3P0 and Queryrunner key code is the DAO layer Oh, remember;

Of course, there are also common methods for extracting utils layers of tools.

1 package Com.bie.dao;
 2 
 3 import java.util.List;
 4 
 5 import com.bie.po.User;
 6 
 7/** 
 8 * @author Biehongli 
 9 * @version created: March 11, 2017 pm 5:46:38 
* * */public
Interfa Ce Userdao {     /***      * Query all user information      * @return
* * 18 Public     list<user> Selectuser ();     
/*** *      Search by number *      @param ID * @return/
25 Public     User selectuserid (int id);     /***      * Search information according to conditions *      @param user      * @return
32      * * Public     list<user> Select (String sql,list<object> List);     
36}
View Code
 1 package Com.bie.dao.impl;
 2 3 Import java.util.List;
 4 5 Import Org.apache.commons.dbutils.QueryRunner;
 6 Import Org.apache.commons.dbutils.handlers.BeanHandler;
 7 Import Org.apache.commons.dbutils.handlers.BeanListHandler;
8 9 Import Com.bie.dao.UserDao;
Ten import Com.bie.po.User;
Import Com.bie.util.BaseUtils; /** * @author Biehongli * @version created: March 11, 2017 pm 5:47:35 * * */public class Userdaoimpl Imple ments userdao{@Override public list<user> Selectuser () {22//Create Queryrunner 23//
Remember that the query is Beanlisthandler differences and additions to the method Beanhandler Queryrunner Qr=baseutils.getqueryrunner ();
try {n String sql= "select * from user";
27//This sentence is equivalent to the following long string of code, which is queryrunner convenient place/*** *user user=new User ();
User.setid (Rs.getint ("id"));
User.setname (rs.getstring ("name")); User.setpassword (Rs.getstring ("Password"));
User.setemail (rs.getstring ("email"));
User.setphone (rs.getstring ("Phone"));
*/Qr.query return (SQL, new Beanlisthandler<user> (User.class)); Exception} catch (e) {e.printstacktrace ();
; @Override public User selectuserid (int id) {46//create Queryrunner Queryrunner
Qr=baseutils.getqueryrunner (); Sql= String "SELECT * from user where id=?
";
The strength of the try {50//Use Queryrunner is here.
Wuyi return Qr.query (sql,new beanhandler<user> (user.class), id);
(Exception e) {e.printstacktrace (););          List<user> (String sql,list<object> List) {60//Create Queryrunner 61
Queryrunner Qr=baseutils.getqueryrunner (); try {63             The first parameter is the incoming SQL, the second is the implementation of the entity class settings, and the third is the set conversion to the array of qr.query (SQL, new beanlisthandler<user> (U
Ser.class), List.toarray ()); Exception} catch (e) {e.printstacktrace ();
; 70} 71 72 73}

Here the Baseutils class is written here, because this is also the core of c3p0 and Queryrunner ...

 1 package com.bie.util;
 2 3 Import java.sql.SQLException;
 4 Import java.util.List;
 5 6 Import Javax.sql.DataSource;
 7 8 Import Org.apache.commons.dbutils.QueryRunner;
9 Import Com.mchange.v2.c3p0.ComboPooledDataSource;  /** * @author Biehongli * @version created: March 11, 2017 1:29:50 15 * Database Connection Tool Class * */public class Baseutils {18 19//Initialize C3P0 private static DataSource datasource=null; static{23//Auto load src directory below
C3P0 configuration file, "C3p0-config.xml" for dataSource = new Combopooleddatasource (); () public static Queryrunner Getqueryrunner () {28///First step: Create Queryrunner object, incoming connection pool object 29// When creating a Queryrunner object, if the incoming data object is DataSource, 30//Then the incoming connection object is not required when using the Queryrunner object method Queryrunner query=new Q
Ueryrunner (DataSource);
32//Second step: Automatically get the connection from the data source (without closing the connection) return query; 34} 35 36/*** 37 * Public methods for adding and deleting changes * @param SQL * @param arr. * @return
* * public static Boolean addupdatedelete (String sql,object[] arr) {Queryrunner Qr=getqueryrunne
R ();
A. int count; try {# count = qr.update (sql, arr); if (count>0) {on-return true
;             }else{(SQLException e) {53};
E.printstacktrace ();
to false; 56} 57 58}

2.5: The completion of the DAO layer and Baseutils layer can be directly in the service layer to delete and modify, the DAO layer will not have to write;

1 package com.bie.service;
 2 
 3 import java.util.List;
 4 
 5 import com.bie.po.User;
 6 
 7/** 
 8 * @author Biehongli 
 9 * @version created: March 11, 2017 pm 7:10:32 
* * */public
Interfa Ce

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.