1Metadata _
1.1 Function _
Using metadata, you can write the parameters of the jdbc precompiled statement and the number and name of the results of the ResultSet .
1.2 Three kinds of metadata
1 ) Database meta object ( DatabaseMetaData )
To know which database to connect to, you must use the metadata object of the database
2 ) Parameter meta object ( ParameterMetaData )
insert into studnetx(xxxx) values(?,?);
After precompiling sql , knowing that the precompiled sql has several parameters, you must use parameter metadata
3 ) The result set meta object (ResultSetMetaData)
ResultSet rs
Know the number of fields in the table, and field names, you must use the metadata of the result set
1.3 Methods
ParameterMetaData pmd = stmt.getParameterMetaData();// Get parameter metadata
int paramCount = pmd.getParameterCount();// Get the number of placeholder parameters
ResultSetMetaData rsmd = rs.getMetaData();// Get the result set metadata of the returned result
int columnCount = rsmd.getColumnCount(); //Get the number of columns
String columnName = rsmd.getColumnName(i); //to the field name
2 DBUtils Tools
2.1 Introduction _
Simple jdbc code encapsulation
2.2 Guide package
commons-dbutils-1.2.jar
2.3 APIs used
QueryRunner class : Through this class, you can perform update operations or query operations.
update(.....): for update operations ( DDL , DML )
query(.....): for query operations ( DQL )
ResultSetHandler interface : used to encapsulate the results after the query.
Object handle(ResultSet rs) : used to encapsulate data
2.4 Methods
QueryRunner qr = new QueryRunner(conn);
qr.update("sql", new Object[]{ parameter list }); // Update query
qr.query("ssql", implementation class of ResultSetHandler interface, new Object[]{parameter list});
Common implementation classes of ResultSetHandler():
ArrayHandler : Encapsulates the data of the first row of the result set into an array of objects.
ArrayListHandler : Encapsulates each row of data in the result set into an array of objects, and puts this array of objects into a List
BeanHandler : encapsulates the first line of data in the result set into a javabean object
BeanListHandler : encapsulates each row of data in the result set into a javabean object , and puts this javabean into the List
Map Handler : Encapsulate the first row of data in the result set into a map object
ScalarHandler : Get the first row and first column of the result set. Typically used for aggregate function queries. For example ( count()/max() )
Note: If the field name of the table is inconsistent with the property name of the javabean , you need to customize the implementation class of ResultSetHandler
3 connection pool
3.1 Introduction _
Store commonly used connection objects ( N for each connection , convenient for different users to connect )
3.2 Common connection pooling tools
Note: The DataSource interface is an interface designed by Sun to standardize the implementation of connection pools.
Connection Pool Tool 1 : DBCP (Datatabse Connection Pool)
Is a product of the Apache organization. Default implementation of connection pool for Tomcat server
Steps for usage:
1 ) Guide package
commons-dbcp-1.4.jar core package
commons-pool-1.5.6.jar auxiliary package
2 ) Create a connection pool object BasicDataSource object
3 ) Set connection parameters ( url , user , password , dirverClass )
4 ) Set connection pool parameters (initial number of connections, maximum number of connections, maximum waiting time)
5 ) Get the connection object ( getConnection() method)
//1) Create a dbcp connection pool object BasicDataSource bds = new BasicDataSource(); BasicDataSource bds = (BasicDataSource)BasicDataSourceFactory.createDataSource(properties配置文件); // The Key of the configuration file should be consistent with the following setxxx . //2) Set the connection parameters bds.setUrl(url); bds.setUsername(user); bds.setPassword(password); bds.setDriverClassName(driverClass);
//3) Set the connection pool parameters bds.setInitialSize(5);//Initialize the connection bds.setMaxActive(10);//Maximum number of connections bds.setMaxWait(3000);//When the maximum number of connections is exceeded, the maximum waiting time is 3 seconds
Connection conn = bds.getConnection(); //Get the connection from the connection pool conn.close();// Put the connection object back into the connection pool. |
Connection Pool Tool 2 : C3P0
Is an open source framework ( hibernate built-in default connection pool tool C3P0) )
Steps for usage:
1 ) Import the jar package of c3p0
c3p0-0.9.1.2.jar core package
2 ) Create a connection pool object ComboPooledDataSource object
3 ) Set connection parameters ( url , user , password , dirverClass )
4 ) Set connection pool parameters (initial number of connections, maximum number of connections, maximum waiting time)
5 ) Get the connection object ( getConnection() method)
//1) Create a connection pool object ComboPooledDataSource cds = new ComboPooledDataSource(); /** * The method of using xml configuration file to read c3p0 * Notice: * 1) A file named c3p0-config.xml needs to be placed in the src directory, and c3p0 will automatically read this file. * 2) The attribute name in the xml configuration file is consistent with the manual setting method name! ! ! ! c3p0 will automatically read */ /** * //1) Create a connection pool object * 1)new ComboPooledDataSource(): Use the construction method without parameters and read the configuration information of default-config * 2) new ComboPooledDataSource (the attribute value of name): Use the parameterized construction method to read the name-config configuration information //2) Set the connection parameters cds.setJdbcUrl(url); cds.setUser(user); cds.setPassword(password); cds.setDriverClass(driverClass);
//3) Set the parameters related to the connection pool cds.setInitialPoolSize(5);//Initialize the number of connections cds.setMaxPoolSize(10);//Maximum number of connections cds.setCheckoutTimeout(3000);//Maximum waiting time cds.setMinPoolSize(3); //Minimum number of connections
//4) Get the connection Connection conn = cds.getConnection(); //5)把连接对象放回连接池中 conn.close();
|
<c3p0-config> <!-- 默认配置 --> <default-config> <!-- 连接参数 --> <property name="jdbcUrl">jdbc:mysql://localhost:3306/day18</property> <property name="user">root</property> <property name="password">root</property> <property name="driverClass">com.mysql.jdbc.Driver</property>
<!-- 连接池参数 --> <property name="initialPoolSize">5</property> <property name="maxPoolSize">12</property> <property name="checkoutTimeout">5000</property> <property name="minPoolSize">3</property>
</default-config> <!-- 命名配置 --> <named-config name="oracle"> <!-- 连接参数 --> <property name="jdbcUrl">jdbc:oracle:thin:@localhost:1521:orcl</property> <property name="user">eric</property> <property name="password">123456</property> <property name="driverClass">oracle.jdbc.driver.OracleDriver</property>
<!-- 连接池参数 --> <property name="initialPoolSize">5</property> <property name="maxPoolSize">12</property> <property name="checkoutTimeout">5000</property> <property name="minPoolSize">3</property> </named-config>
</c3p0-config>
|