First import the Dbutils toolkit: Commons-dbutils-1.6.jar
And then import the C3P0 Toolkit: C3p0-0.9.1.2.jar
First create the C3P0 tool class:
Package star.july.util;
Import java.sql.Connection;
Import java.sql.SQLException;
Import Javax.sql.DataSource;
Import Com.mchange.v2.c3p0.ComboPooledDataSource;
public class C3p0util {
private static Combopooleddatasource ds = new Combopooleddatasource ();
public static Connection getconnection () {
try{
Connection conn = Ds.getconnection ();
return conn;
} catch (SQLException e) {
e.printstacktrace ();
throw new RuntimeException ();
}
public static DataSource Getdatasource () {return
ds;
}
}
Configure the C3p0 zml file, the filename must be: c3p0-config.xml
<c3p0-config> <!--default configuration--> <default-config> <!--property name is the setter method name--> <property name= "D Riverclass ">com.mysql.jdbc.Driver</property> <property name=" Jdbcurl ">jdbc:mysql://localhost:3306 /day19</property> <property name= "user" >root</property> <property name= "password" >root< /property> <property name= "initialpoolsize" >5</property> <property name= "Maxpoolsize" >12</ property> <property name= "Checkouttimeout" >3000</property> </default-config> <!--naming configuration
; <named-config name= "Day17" > <property name= "Driverclass" >com.mysql.jdbc.Driver</property> < Property Name= "Jdbcurl" >jdbc:mysql://localhost:3306/day17</property> <property name= "user" >root </property> <property name= "password" >root</property> <property name= "Initialpoolsize" >5&L t;/property> <property name= "Maxpoolsize" ≫15</property> <property name= "checkouttimeout" >3000</property> </named-config> </ C3p0-config>
Create a student class again
Package star.july.dbutil;
public class Student {
private int id;
private String name;
private String gender;
public int getId () {return
ID;
}
public void setId (int id) {
this.id = ID;
}
Public String GetName () {return
name;
}
public void SetName (String name) {
this.name = name;
}
Public String Getgender () {return
gender;
}
public void Setgender (String gender) {
this.gender = gender;
}
@Override public
String toString () {return
"Student [id= + ID +", name= "+ name +", gender= "+ gender
+ "]";
}
}
To demonstrate the update action for the Dbutils tool:
Package star.july.dbutil;
Import java.sql.Connection;
Import java.sql.SQLException;
Import Org.apache.commons.dbutils.QueryRunner;
Import Org.apache.commons.dbutils.handlers.BeanHandler;
Import Org.junit.Test;
Import Star.july.util.C3P0Util;
Demo Dbutils Tool update operations public class Demo1 {//Create main program Queryrunner QR = new Queryrunner (C3p0util.getdatasource ()); Queryrunner qr2 = new Queryrunner (); No datasource//No transaction operation @Test public void Test1 () throws exception{//qr.update (INSERT into student (Name,gender) Valu
ES (?,?) "," Dog Eggs "," female ");
Qr.update ("Delete from student where id =?", 4);
System.out.println ("Affected" +count+ "line");
//There is a transaction operation @Test public void Test2 () {Connection conn = c3p0util.getconnection ();
try{//Get Connection object//1. Open transaction Conn.setautocommit (FALSE);
Qr2.update (conn, "Delete from student where id=?", 2);
int i =100/0;
Qr2.update (conn, "Delete from student where id=?", 3);
Submit transaction Conn.commit (); }catch (Exception e) {E.prinTstacktrace ();
Rollback TRANSACTION try {conn.rollback ();
catch (SQLException E1) {e1.printstacktrace ();
}
}
}
}
Query class operations
Package star.july.dbutil;
Import java.util.List;
Import Org.apache.commons.dbutils.QueryRunner;
Import Org.apache.commons.dbutils.handlers.ArrayHandler;
Import Org.apache.commons.dbutils.handlers.ArrayListHandler;
Import Org.apache.commons.dbutils.handlers.BeanHandler;
Import Org.apache.commons.dbutils.handlers.BeanListHandler;
Import Org.apache.commons.dbutils.handlers.ScalarHandler;
Import Org.junit.Test;
Import Star.july.util.C3P0Util;
Query class Operation public class Demo2 {queryrunner QR = new Queryrunner (C3p0util.getdatasource ()); Query a data: Beanhandler, encapsulate a result set with a JavaBean object/convention: The table's field name and JavaBean's property name are consistent @Test public void test1 () throws exception{S
Tudent stu = Qr.query ("SELECT * from student where id =?", new Beanhandler (Student.class), 1);
System.out.println (Stu);
}//Query multiple data: Beanlisthandler: Wrap a result set in a list collection containing multiple JavaBean objects/Conventions: The field names of the tables are consistent with the JavaBean property names. @Test public void Test2 () throws exception{list<student> List = Qr.query ("select* from Student", New BeanlisthanDler (Student.class));
for (Student stu:list) {System.out.println (STU); }//Query a data: Arrayhandler: Encapsulate a result set into an object array @Test public void Test3 () throws exception{object[] array = qr.query ("Selec
T * from student where id =? ", new Arrayhandler (), 5);
for (int i = 0; I <array.length;i++) {System.out.print (array[i]); }//Query multiple data: Arraylisthandler: Encapsulates a result set with a List (containing multiple object arrays) @Test public void Test4 () throws exception{list<object[]& Gt
List = Qr.query ("SELECT * FROM Student", New Arraylisthandler ());
For (object[] obj:list) {for (Object stu:obj) {System.out.print (stu+ "\ t");
} System.out.println (); }//query for one record (and only one field): Aggregate query Count (*) max () min () avg (): Scalarhandler: Encapsulates an object @Test public void Test5 () throws Excepti
on{Long count = Qr.query ("SELECT count (*) from student", New Scalarhandler ());
System.out.println (count);
}
}