Use tomcat5.0 to bring your own connection pool

Source: Internet
Author: User
Tags connection pooling getmessage odbc sql string sql error stmt access

Have recently been interested in connection pooling, try it on their own hands, this thought that they write the connection pool no problem, the results and students exchange, he said that although I wrote the connection pool, but in the programming and did not use, I am lazy, so there is no modification, want to directly use Tomcat connection pool is good.

First modify the Server.xml file:

<context path= "" docbase= "My Site" debug= "0" reloadable= "true" crosscontext= "true" privileged= "true" >
<resource name= "jdbc/access" auth= "Container" type= "Javax.sql.DataSource"/>
<resourceparams name= "Jdbc/access" >
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>sun.jdbc.odbc.JdbcOdbcDriver</value>
</parameter>
<parameter>
<name>url</name>
<value>jdbc:odbc:patent</value>
</parameter>
<parameter>
<name>username</name>
<value></value>
</parameter>
<parameter>
<name>password</name>
<value></value>
</parameter>
<parameter>
<name>maxActive</name>
<value>20</value>
</parameter>
<parameter>
<name>maxIdle</name>
<value>10</value>
</parameter>
<parameter>
<name>maxWait</name>
<value>-1</value>
</parameter>
</ResourceParams>
</Context>

Originally thought that uses access also to download the driver, actually has brought, has harmed me also to surf the internet to look for a long time, because the SQL needs to download the database drive by itself.

After modifying the configuration file, you can use the connection pool for database operations, you write a bean, you can use it more convenient:

Package SQL;
Import Javax.naming.Context;
Import Javax.sql.DataSource;
Import Javax.naming.InitialContext;
Import java.sql.*;
Import javax.sql.*;

public class Dbpool
{

DataSource ds = null;
Connection conn = null;
ResultSet rs = null;
Statement Stmt=null;
Public Dbpool () {
Try
{
Context Initctx = new InitialContext ();
Context Envctx = (context) initctx.lookup ("java:comp/env");
ds = (DataSource) envctx.lookup ("jdbc/access");
Conn=ds.getconnection ();
Stmt=conn.createstatement ();
}
catch (SQLException e)
{
System.err.println ("DB SQL Error:" +e.getmessage ());
}
catch (Exception e) {
System.err.println ("Dbpool ():" +e.getmessage ());
}
}

public boolean executeupdate (String sql) {
Try
{
Stmt.executeupdate (SQL);
return true;
}
catch (SQLException e)
{
System.err.println ("Executeupdate:" +e.getmessage ());
}
return false;
}

Public ResultSet executequery (String sql) {
Rs=null;
Try
{
Rs=stmt.executequery (SQL);
}
catch (SQLException ex)
{
System.err.println ("ExecuteQuery:" +ex.getmessage ());
}
Return RS;
}

public void Close () {
Try
{
Rs.close ();
Stmt.close ();
Conn.close ();
}
catch (Exception e)
{
System.out.println (E.tostring ());
}
}
}

The compilation can be used, do a page test:

<%@ page contenttype= "text/html; charset=gb2312 "language=" java "import=" java.sql.* "errorpage=" "%>"
<jsp:usebean id= "Data" scope= "page" class= SQL. Dbpool "/>

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "http://www.w3.org/TR/html4/loose.dtd" >
<title>untitled document</title>
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">

<body>
<table width= "100%" border= "1" >
<tr>
<TD align= "center" > title </td>
<TD width= "30%" align= "Center" > Release Time </td>
</tr>
<%
ResultSet rs = null;
rs = Data.executequery ("Select Tittle,publictime from News");
while (Rs.next ()) {
String tittle=rs.getstring ("tittle");
String ptime=rs.getstring ("Publictime");
%>

<tr>
<TD align= "center" ><%=tittle%></td>
<TD align= "center" ><%=ptime.substring (0,10)%></td>
</tr>
<%
}
%>
</table>
</body>
<%
Data.close ();
%>

Page normal display, through!! Feel comfortable, go back to continue to change my own write the connection pool, whistling!!!



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.