First step: Import the C3P0 package
Step two: Under the Classpath directory, create a C3p0-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
<!--default configuration, can only occur once--
<default-config>
<!--connection Timeout setting 30 seconds--
<property name="Checkouttimeout">30000</property>
<!--30 seconds to check connection's free--
<property name="Idleconnectiontestperiod">30</property>
<!--initialized Pool size--
<property name="initialpoolsize">2</property>
<!--up to one connection idle time-
<property name="MaxIdleTime">30</property>
<!--up to how many connections connection
<property name="maxpoolsize">10</property>
< A few connections in the pool with!--minimum
<property name="minpoolsize">2</property>
<!--batch-processed statements
-
<property name="maxstatements">50</property>
<!--a few connections per increase
<property name="acquireincrement">3</property>
<property name="Driverclass">com.mysql.jdbc.Driver</property>
<property name="Jdbcurl">
<! [cdata[jdbc:mysql://127.0.0.1:3306/db909?useunicode=true&characterencoding=utf-8]]>
</property>
<property name="user">root</property>
<property name="password">1234</property>
</default-config>
</c3p0-config>
Step three: Create a factory class to get this connection
Package cn.itcast.utils;
import java.sql.Connection;
import Javax.sql.DataSource;
import Com.mchange.v2.c3p0.ComboPooledDataSource;
Public class datasourceutils {
Private Static DataSource DS;
Static {
ds =//default configuration in Read C3p0-config.xml
New Combopooleddatasource ();
}
Public Static DataSource Getdatassource () {
return DS;
}
Public Static Connection Getconn () {
Connection con = null;
Try {
con = ds. getconnection ();//each time a new connection is obtained from the DS
}catch(Exception e) {
E.printstacktrace ();
}
return con;
}
}
Combopooleddatasource has three constructs:
With no parameters.
Receives a Boolean
By default, True, that is, all connection.autocommit properties are true.
That receives a string of
In a c3p0-config.xml file, you can configure multiple connections. In addition to the default connection, the others are called named Connections. Pass
<named-config name= "xxxx"/>
Specify a named connection:
<!--default configuration, can only occur once--
<named-config name="db909">
<property name="Checkouttimeout">1000</property>
<property name="Idleconnectiontestperiod">30</property>
<property name="initialpoolsize">2</property>
<property name="MaxIdleTime">30</property>
<property name="maxpoolsize">5</property>
<property name="minpoolsize">2</property>
<property name="maxstatements">50</property>
<property name="acquireincrement">3</property>
<property name="Driverclass">com.mysql.jdbc.Driver</property>
<property name="Jdbcurl">
<! [cdata[jdbc:mysql://127.0.0.1:3306/db909?useunicode=true&characterencoding=utf-8]]>
</property>
<property name="user">root</property>
<property name="password">1234</property>
</named-config>
Connect to the database through a named connection in your code:
ds =
New Combopooleddatasource ("db909");
Summarize:
C3p0 The connection pool, the connection is wrapped only when the user obtains the connection.
Meta Data analysis
metadata, which is the analysis of all the information in a database, is the case where only one object is owned by connection.
Databasemetadate-describes the database information.
Resultsetmetadate-The type information that describes the result of the data. Core.
If you want to perform meta-data analysis, you must use Statement,preparedstatement.
list<map> list = Run.query ("SELECT * from users", New Maplisthandler ());
[{id= "U001", name= "Jack", pwd= "ddd"} ....]
list<bean> list = Run.query ("SELECT * from users", New Beanlisthanderl<user> (User.class));
[USER=[ID=DD],,,,,]
Exports all the tables and data from a specified database to Excel.
CREATE VIEW UC As
SELECT u.name as uname,c.name as CNAME
From the users U INNER JOIN contacts C on U.id=c.uid;
1. Analyze database data with Databasemetadate
Public void dbm () throws exception{
Connection con = datasourceutils. Getconn ();
DatabaseMetaData dm= con.getmetadata ();
ResultSet rs= Dm.getcatalogs ();//Get all database names
while (Rs.next ()) {
String name = rs.getstring ("Table_cat");
SYSTEM.ERR.PRINTLN (name);
// }
System.err.println ("======================");
String dbName = Dm.getdatabaseproductname ();//Database name
System. err. println (DbName);
System. err. println ("How many tables are in the database:");
ResultSet rs2 = dm.gettables ("db909", "db909",null,new string[]{"TABLE"});
while (Rs2.next ()) {
String tableName = rs2.getstring ("table_name");
System. err. println (TableName);
}
}
2. Analyze the result set with Resultsetmetadate
This class is used to parse the result set of a query:
How many columns, column names, and types of columns are analyzed?
@Test
Public void rs2 () throws exception{
Connection con = datasourceutils. Getconn ();
Go to the exam database.
Statement st = Con.createstatement ();
St.execute ("Use exam");
Inquire
String sql = "SELECT * FROM dept";
ResultSet rs = st.executequery (SQL);
Analysis of RS result set
ResultSetMetaData Rsmd=rs.getmetadata ();
Gets a few columns
int cols = Rsmd.getcolumncount ();
System. err. println (cols);
Get the name of each field
list<string> colnames = new arraylist<string> ();//Save all fields
for (int i=0;i<cols;i++) {
String colname = Rsmd.getcolumnname (i+1);
System. err. Print (colname+ "\t\t");
colNames.Add (colname);
}
System. err. println ();
Get Data
while (Rs.next ()) {
for (String nm:colnames) {//Iterate through the columns in a row
String val = rs.getstring (nm);
System. err. Print (val+ "\t\t");
}
System. err. println ();
}
Con.close ();
}
Java Learning Note-c3p0 connection pooling and meta data Analysis (42)