使用TOMCAT5.0自带的连接池
类别: JSP教程
近来对连接池产生了兴趣,就自己动手试了试,本以为自己写的连接池没有问题,结果和同学交流,他说我虽然写了连接池,可是在编程时并没有用到,本人比较懒,所以就没有修改,想直接使用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();
%>
页面正常显示,通过!!心情舒畅,回去继续改我自己写的连接池,呼呼!!!
首先修改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();
%>
页面正常显示,通过!!心情舒畅,回去继续改我自己写的连接池,呼呼!!!
- 上一篇: TOMCAT的初学体验
- 下一篇: 现场纪实-如何入侵基于JSP的网站
-= 资 源 教 程 =-
文 章 搜 索