Java learning notes-c3p0 connection pool and metadata analysis (42), learning notes c3p0
Step 1: import the c3p0 package
Step 2: Create a c3p0-config.xml under the classpath directory
<? Xml version ="1.0"Encoding =UTF-8"?>
<C3p0-config>
<! -- Default configuration, which can only appear once -->
<Default-config>
<! -- Set connection timeout to 30 seconds -->
<Property name ="CheckoutTimeout"& Gt; 30000 </property>
<! -- Check the idle connection once in 30 seconds -->
<Property name ="IdleConnectionTestPeriod"> 30 </property>
<! -- Size of the initialized pool -->
<Property name ="InitialPoolSize"> 2 </property>
<! -- The maximum idle time of a connection -->
<Property name ="MaxIdleTime"> 30 </property>
<! -- Maximum number of connections allowed -->
<Property name ="MaxPoolSize"> 10 </property>
<! -- The minimum pool has several connections -->
<Property name ="MinPoolSize"> 2 </property>
<! -- Batch statement
-->
<Property name ="MaxStatements"> 50 </property>
<! -- Increase several connections each time -->
<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"& Gt; 1234 </property>
</Default-config>
C3p0-config>
Step 3: Create a factory class to obtain the connection
PackageCn. itcast. utils;
ImportJava. SQL. Connection;
ImportJavax. SQL. DataSource;
ImportCom. mchange. v2.c3p0. ComboPooledDataSource;
Public ClassDataSourceUtils {
Private StaticDataSourceDs;
Static{
Ds= // Default read c3p0-config.xml Default Configuration
NewComboPooledDataSource ();
}
Public StaticDataSource getDatasSource (){
Return Ds;
}
Public StaticConnection getConn (){
Connection con =Null;
Try{
Con =Ds. GetConnection (); // each time a new connection is obtained from ds
}Catch(Exception e ){
E. printStackTrace ();
}
ReturnCon;
}
}
ComboPooledDataSource has three structures:
No parameter.
Receives a boolean
By default, true indicates that all connection. autoCommit attributes are true.
Receives a string
In a c3p0-config.xml file, you can configure multiple connections. All connections except the default connections are named connections. Pass
<Named-config name = "xxxx"/>
Specify a named connection:
<! -- Default configuration, which can only appear once -->
<Named-config name ="Db909">
<Property name ="CheckoutTimeout"& Gt; 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"& Gt; 1234 </property>
</Named-config>
Connect to the database by name in the Code:
Ds=
NewComboPooledDataSource ("db909 ");
Summary:
C3p0 Connection pool. Connection is encapsulated only when the user obtains the Connection.
Metadata Analysis
Metadata is used to analyze all the information of a database when only one object is connected.
DataBaseMetadate-indicates the database information.
ResultSetMetadate-specifies the type of the data result. Core.
To perform metadata analysis, you must use statement and 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],]
Export all tables and data in a specified database to excel.
Create view uc
SELECT u. name AS uname, c. name AS cname
FROM users u inner join contacts c ON u. id = c. uid;
1. Use databasemetadate to analyze database data
Public VoidDbm ()ThrowsException {
Connection con = performanceutils.GetConn();
DatabaseMetaData dm = con. getMetaData ();
// ResultSet rs = dm. getCatalogs (); // obtain 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,NewString [] {"TABLE "});
While(Rs2.next ()){
String tableName = rs2.getString ("TABLE_NAME ");
System.Err. Println (tableName );
}
}
2. Use ResultSetMetadate to analyze the result set
This class is used to analyze the query result set:
There are several columns in the analysis. What are the column types?
@ Test
Public VoidRs2 ()ThrowsException {
Connection con = performanceutils.GetConn();
// Go to exam Database
Statement st = con. createStatement ();
St.exe cute ("use exam ");
// Query
String SQL = "select * from dept ";
ResultSet rs = st.exe cuteQuery (SQL );
// Analyze the rs result set
ResultSetMetaData rsmd = rs. getMetaData ();
// Obtain several columns
IntCols = rsmd. getColumnCount ();
System.Err. Println (cols );
// Obtain the name of each field
List <String> colNames =NewArrayList <String> (); // save all fields
For(IntI = 0; I <cols; I ++ ){
String colName = rsmd. getColumnName (I + 1 );
System.Err. Print (colName + "\ t ");
ColNames. add (colName );
}
System.Err. Println ();
// Obtain data
While(Rs. next ()){
For(String nm: colNames) {// traverse the columns in a row
String val = rs. getString (nm );
System.Err. Print (val + "\ t ");
}
System.Err. Println ();
}
Con. close ();
}