近来对连接池产生了兴趣,就自己动手试了试,本以为自己写的连接池没有问题,结果和同学交流,他说我虽然写了连接池,可是在编程时并没有用到,本人比较懒,所以就没有修改,想直接使用tomcat的连接池就好.
首先修改server.xml文件:
<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>
原以为用access还要下载驱动程序,其实自带了,害得我还上网找了好久,因为sql就需要自己去下载数据库驱动.
修改完配置文件,就可以使用连接池进行数据库操作了,自己写了个bean,使用起来能方便些:
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());
}
}
}
编译通过就可以使用了,做个页面测试一下:
<%@ 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">
<html>
<head>
<title>untitled document</title>
<meta http-equiv="content-type" content="text/html; charset=gb2312">
</head>
<body>
<table width="100%" border="1">
<tr>
<td align="center">标 题</td>
<td width="30%" align="center">发布时间</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>
</html>
<%
data.close();
%>
页面正常显示,通过!!心情舒畅,回去继续改我自己写的连接池,呼呼!!!
新闻热点
疑难解答