JDBC data sources and connection pooling
Database connection pooling is the creation of enough database connection pools when the application starts, requiring access in Java programs
Database, you can get an idle connection from the data source and run out of time in the return to the connection pool
Instance:
+mysql5.5 the configuration of data sources and connection pools in Tomcat6.0
(1) Copy the JDBC driver of the database to the <catalina_home>/common/lib directory
(2) Configure the data source and connection pool in <catalina_home>/conf/server.xml, for example
<context path= "/jsp_jdbc" docbase= "Jsp_jdbc" debug= "0" reloadable= "true" >
<resourceparams name= "Jdbc/mydatasource" auth= "Container" type= "Javax.sql.DataSource"/>
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>
<parameter>
<name>maxActive</name>
<value>1000</value>
</parameter>
<parameter>
<name>maxIdle</name>
<value>30</value>
</parameter>
<parameter>
<name>maxWait</name>
<value>10000</value>
</parameter>
<parameter>
<name>username</name>
<value>root</value>
</parameter>
<parameter>
<name>password</name>
<value>root</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>com.mysql.jdbc.Driver</value>
</parameter>
<parameter>
<name>url</name>
<value>jdbc:mysql://localhost/test?autoReconnect=true</value>
</parameter>
<ResourceParams>
<Context>
To configure a reference to a data source connection pool in the Web. xml file
<resource-ref>
<description>db connection</description>
<res-ref-name>jdbc/myDataSource</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
Accessing a database through a data source in a JSP or servlet
<body>
Accessing a database from a data source
<%
Connection Conn=null;
Statement Stat=null;
ResultSet Rs=null;
Getting a connection from a data source connection pool
Context ctx=new InitialContext ();
DataSource ds= (DataSource) ctx.lookup ("Java:comp/env/jdbc/mydatasource");
Conn=ds.getconnection ();
Querying data tables
Stat=conn.createstatement ();
String sql= "SELECT * from user";
Rs.stat.executeQuery (SQL);
Output query results to interface
while (Rs.next ()) {
Out.println ("<li> Account:" +rs.getstring (2). Trim ());
Out.println ("Password:" +rs.getstring (3). Trim () + "</li>");
}
Close the connection and release the resource
Rs.close ();
Stat.close ();
Conn.close ();
%>
</body>
JDBC data sources and connection pooling