当前位置: 首页 > 图文教程 > 网络编程 > JSP > 数据库BEAN:RESIN连接池

JSP
我认为JSP有问题(上)
我认为JSP有问题(下)
jsp“抓”网页代码的程序
关于在bean里面打印html的利弊看法
bean里面如何打印到html页面
jdbc3中的RowSet 接口规范
Apusic Application Server1.0中jsp源代码泄漏漏洞
Unify的eWave ServletExec拒绝服务漏洞
通过提交超长的GET请求导致IBM HTTP Server远程溢出
在HTTP请求中添加特殊字符导致暴露JSP源代码文件
Resin 1.2 重要源代码暴露漏洞
多中WEB服务器的通用JSp源代码暴露漏洞
Tomcat 暴露JSP文件内容
IBM WebSphere Application Server 暴露JSP文件内容
JRun 2.3.x 范例文件暴露站点安全信息
BEA WebLogic 暴露源代码漏洞
IBM WebSphere Application Server 3.0.2 存在暴露源代码漏洞
Tomcat 3.1 存在暴露网站路径问题
Sun Java Web Server 能让攻击者远程执行任意命令
Netscape 修复 JAVA 安全漏洞

JSP 中的 数据库BEAN:RESIN连接池


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-03   浏览: 77 ::
收藏到网摘: n/a

数据库BEAN:RESIN连接池
matrix原创 作者:fajaven
JSP初学者一用到数据库都会遇到数据库连接BEAN的问题。
并且对初学者也推荐用RESIN做服务器,配置简单(开发不需要配置APACHE)。
所以把我自己在用的数据库连接BEAN发出来来,供参考。
由于RESI本身也提供了连接池,就更省事了,访问量不大时都够了。

欢迎指正与优化。谢谢!!!
==============================
/*****************************
* 数据库连接 bean ,用来连接 resin 的连接池
*****************************/
package net.asales.mysql;

import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.sql.DataSource;
import javax.naming.Context;
import javax.naming.InitialContext;

public class DBConnection {
private Connection conn = null;
private Statement stmt = null;
private ResultSet rs = null;
private int resultNum = 0;

/**
* 构造函数
* 找到数据源,并用这个数据源创建连接
*/
public DBConnection() {
try {
Context env = new InitialContext();
DataSource pool = (DataSource) env.lookup("java:comp/env/jdbc/asales");
if (pool == null)
throw new Exception("jdbc/asales is an unknown DataSource");
conn = pool.getConnection();
stmt = conn.createStatement();
} catch (Exception e) {
System.out.println("naming:" + e.getMessage());
}
}

/**
* 执行SQL语句:查询记录
* @param sql SQL语句
* @return ResultSet 记录集
*/
public ResultSet executeQuery(String sql) {
rs = null;
try {
rs = stmt.executeQuery(sql);
} catch(SQLException se) {
System.out.println("Query error:" + se.getMessage());
}
return rs;
}

/**
* 执行SQL语句 :插入与更新记录
* @param sql SQL语句
* @return int resultNum 更新的记录数
*/
public int executeUpdate(String sql) {
resultNum=0;
try {
resultNum = stmt.executeUpdate(sql);
} catch (SQLException se) {
System.err.println("Update error:" + se.getMessage());
}
return resultNum;
}

/**
* 关闭连接
*/
public void close() {
try {
if (rs != null) {
rs.close();
rs = null;
}
if (stmt != null) {
stmt.close();
stmt = null;
}
if (conn != null) {
conn.close();
conn = null;
}
} catch (SQLException se) {
System.out.println("close error: " + se.getMessage());
}
}
}



===================
以下是RESIN配置文件中关于数据源的配置例:

<resource-ref>
<res-ref-name>jdbc/asales</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<init-param driver-name="org.gjt.mm.mysql.Driver"/>
<init-param url="jdbc:mysql://localhost:3306/asales"/>
<init-param user="root"/>
<init-param password=""/>
<init-param max-connections="20"/>
<init-param max-idle-time="30"/>
</resource-ref>



欢迎大家指正与优化。谢谢!!

matrix开源技术原创
如果你对此文章有任何看法或建议,请到Matrix论坛发表您的意见.
您也可以点击-fajaven查看翻译作者的详细信息.