On the basis of jdbc, advanced step C3p0 connection pool (DBCP cannot read xml configuration files and has been eliminated) and use of two main classes of QueryRunner and ResultSetHandler in DBUtils,

Source: Internet
Author: User

On the basis of jdbc, advanced step C3p0 connection pool (DBCP cannot read xml configuration files and has been eliminated) and use of two main classes of QueryRunner and ResultSetHandler in DBUtils,

First, check the C3p0 connection pool. The biggest advantage is that the default configuration file can be automatically read.

<?xml version="1.0" encoding="UTF-8"?><c3p0-config>    <default-config>        <property name="driverClass">com.mysql.jdbc.Driver</property>        <property name="jdbcUrl">jdbc:mysql://localhost:3306/swift_db        </property>        <property name="user">root</property>        <property name="password">root</property>        <property name="initialPoolSize">20</property>        <property name="acquireIncrement">5</property>        <property name="maxPoolSize">50</property>        <property name="minPoolSize">5</property>    </default-config></c3p0-config>

The configuration file contains four General Master options and some other configurations.

You only need to use the implementation class ComboPooledDataSource of javax. SQL. DateSource in C3p0 to create an object.

Private static ComboPooledDataSource dataSource = new ComboPooledDataSource ();

If the new parameter is null, the xml configuration file is automatically called. The label <default-config> is required for the configuration file.

You can also call the named configuration file. The configuration file <named-config> is required.

<?xml version="1.0" encoding="UTF-8"?><c3p0-config>    <named-config>        <property name="driverClass">com.mysql.jdbc.Driver</property>        <property name="jdbcUrl">jdbc:mysql://localhost:3306/swift_db        </property>        <property name="user">root</property>        <property name="password">root</property>        <property name="initialPoolSize">20</property>        <property name="acquireIncrement">5</property>        <property name="maxPoolSize">50</property>        <property name="minPoolSize">5</property>    </named-config></c3p0-config>

C3p0 is generally used as a Tool class for convenient calling.

Package com. swift. base; import java. SQL. connection; import java. SQL. resultSet; import java. SQL. SQLException; import java. SQL. statement; import javax. SQL. dataSource; import com. mchange. v2.c3p0. comboPooledDataSource; public class C3p0Utils {// the function of the data source connection pool c3p0 does not need to establish a connection every time, wasting resources and waiting time, and placing the connection in the connection pool, when needed get // c3p0 connection pool can be automatically read through the configuration file c3p0-config.xml file related properties // c3p0 connection pool is java javax. SQL. dataSource implementation class, which must implement the getConnection method private Static combooleddatasource dataSource = new ComboPooledDataSource ("swift"); // public static DataSource getDataSource () {return dataSource ;} // obtain the public static Connection getConnection () throws Exception {dataSource for a conn resource from the Connection pool. setDriverClass ("com. mysql. jdbc. driver "); dataSource. setJdbcUrl ("jdbc: mysql: // localhost: 3306/swift_db"); dataSource. setUser ("root"); dataSource. setPass Word ("root"); dataSource. setAcquireIncrement (5); dataSource. setInitialPoolSize (20); dataSource. setMinPoolSize (5); dataSource. setMaxPoolSize (50); Connection conn = dataSource. getConnection (); return conn;} public static void close (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 ();}}}}

The xml configuration file has been called, and attribute configuration will be performed every time the Connection is called. This is a waste of resources. You can use a static code block to call it only once.

Package com. swift. jinji; import java. beans. propertyVetoException; import java. SQL. connection; import java. SQL. SQLException; import javax. SQL. dataSource; import com. mchange. v2.c3p0. comboPooledDataSource;/* use C3P0 to obtain 10 connection objects. Required: Do not use the configuration file */public class C3p0_10con {private static combooleddatasource dataSource = new ComboPooledDataSource (); static {try {dataSource. setDriverClass ("com. mysql. jdbc. driver ");} c Atch (PropertyVetoException e) {e. printStackTrace ();} dataSource. setJdbcUrl ("jdbc: mysql: // localhost: 3306/swift_db"); dataSource. setUser ("root"); dataSource. setPassword ("root"); dataSource. setInitialPoolSize (20); dataSource. setAcquireIncrement (5); dataSource. setMaxPoolSize (50); dataSource. setMinPoolSize (5);} public static void main (String [] args) throws PropertyVetoException {for (int I = 0; I <10; I ++) {System. out. println (C3p0_10con.getConnection () ;}} public static DataSource getDataSource () {return dataSource;} public static Connection getConnection () {Connection con = null; try {con = dataSource. getConnection ();} catch (SQLException e) {e. printStackTrace ();} return con;} public static void close (Connection con) {if (con! = Null) {try {con. close () ;}catch (SQLException e) {e. printStackTrace ();}}}}

It is used with DBUtils's QureyRunner class and ResultSetHandler class to facilitate addition, deletion, modification, and query of databases.

Package com. swift. jinji; import java. SQL. SQLException; import java. util. arrays; import java. util. list; import java. util. map; import org. apache. commons. dbutils. queryRunner; import org. apache. commons. dbutils. resultSetHandler; import org. apache. commons. dbutils. handlers. arrayHandler; import org. apache. commons. dbutils. handlers. arrayListHandler; import org. apache. commons. dbutils. handlers. beanHandler; import org. ap Ache. commons. dbutils. handlers. columnListHandler; import org. apache. commons. dbutils. handlers. keyedHandler; import org. apache. commons. dbutils. handlers. mapHandler; import org. apache. commons. dbutils. handlers. mapListHandler; import org. apache. commons. dbutils. handlers. scalarHandler; import com. swift. base. c3p0Utils; import com. swift. domain. puppy;/* query the first data in the User table. And encapsulate the data into an array of objects. */Public class Qurey_Puppy {public static void main (String [] args) {QueryRunner qr = new QueryRunner (C3p0Utils. getDataSource (); String SQL = "select * from puppy;"; ResultSetHandler <Puppy> rsh = new BeanHandler <Puppy> (Puppy. class); ResultSetHandler <Object []> rsh1 = new ArrayHandler (); ResultSetHandler <List <Object []> rsh2 = new ArrayListHandler (); resultSetHandler <Object> rsh3 = new ScalarHandler ("name"); ResultSetHandler <Map <String, Object> rsh4 = new MapHandler (); ResultSetHandler <List <Map <String, object >>> rsh5 = new MapListHandler (); ResultSetHandler <List <Object> rsh6 = new ColumnListHandler ("name"); ResultSetHandler <Map <Object, Map <String, object >>> rsh7 = new KeyedHandler ("id"); try {Map <Object, Map <String, Object> result = qr. query (SQL, rsh7);/* for (Object [] puppy: dog) {System. out. println (Arrays. toString (puppy);} * // System. out. println (result);} catch (SQLException e) {e. printStackTrace ();}}}

ResultSetHandler has many sub-classes that can be placed in various containers. After the data is retrieved from the database, the data is put into the object array, array set, object element, Map set, Map set list, and Object List, in Map

 

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.