日期:2014-05-18  浏览次数:20707 次

谁能给俺传一份好的jsp连接数据库的bean,最好能释放资源。没分啦,全部赠送
我的邮箱:
yuyangwxj@163.com

先谢谢啦!

------解决方案--------------------
http://www.jdon.com/designpatterns/designpattern_Facade.htm
数据库操作可以中WEB开发中最常用到的,很多Java开发工具都提供了自动的Data bean WinZard.只要数据库建立好,相应的操作数据库的Bean就基本可以自动完成,本人使用Jcreator开发bean,手工录入觉得也不是很麻烦的事情,下面我常用的数据库操作bean,完全可以对付访问量不是很大的系统 :

Mysql类:

import java.sql.*;
import java.io.*;
/**
* 处理数据库的连接和访问
* @author sanware bqlr
* @version 1.01
*/
public class Mysql {

private Connection conn = null;
private Statement stmt = null;
private PreparedStatement prepstmt = null;

//这是一个全局类,里面放置数据库的参数,如数据库主机 访问用户名 密码等
private static BeansConstants CONST = BeansConstants.getInstance();

/**
* 构造数据库的连接和访问类
*/
public Mysql() throws Exception {
Class.forName(CONST.dbdriver);
conn = DriverManager.getConnection(CONST.dburl);
stmt = conn.createStatement();
}
public Mysql(String sql) throws Exception {
Class.forName(CONST.dbdriver);
conn = DriverManager.getConnection(CONST.dburl);
this.prepareStatement(sql);
}

/**
* 返回连接
* @return Connection 连接
*/
public Connection getConnection() {
return conn;
}
/**
* PreparedStatement
* @return sql 预设SQL语句
*/
public void prepareStatement(String sql) throws SQLException {
prepstmt = conn.prepareStatement(sql);
}
/**
* 设置对应值
* @param index 参数索引
* @param value 对应值
*/
public void setString(int index,String value) throws SQLException {
prepstmt.setString(index,value);
}
public void setInt(int index,int value) throws SQLException {
prepstmt.setInt(index,value);
}
public void setBoolean(int index,boolean value) throws SQLException {
prepstmt.setBoolean(index,value);
}
public void setDate(int index,Date value) throws SQLException {
prepstmt.setDate(index,value);
}
public void setLong(int index,long value) throws SQLException {
prepstmt.setLong(index,value);
}
public void setFloat(int index,float value) throws SQLException {
prepstmt.setFloat(index,value);
}
//File file = new File( "test/data.txt ");
//int fileLength = file.length();
//InputStream fin = new java.io.FileInputStream(file);
//mysql.setBinaryStream(5,fin,fileLength);
public void setBinaryStream(int index,InputStream in,int length) throws SQLException {
prepstmt.setBinaryStream(index,in,length);
}

public void clearParameters()
throws SQLException
{
prepstmt.clearParameters();
}
/**
* 返回预设状态
*/
public PreparedStatement getPreparedStatement() {
return prepstmt;
}
/**
* 返回状态
* @return Statement 状态
*/
public Statement getStatement() {
return stmt;
}
/**
* 执行SQL语句返回字段集
* @param sql SQL语句
* @return ResultSet 字段集
*/
public ResultSet executeQuery(String sql) throws SQLException {
if (stmt != null) {
return stmt.executeQuery(sql);
}
else return null;
}
public ResultSet executeQuery() throws SQLException {
if (prepstmt != null) {
return prepstmt.executeQuery();
}
else return null;
}
/**
* 执行SQL语句
* @param sql SQL语句
*/
public void executeUpdate(String sql) throws SQLException {
if (stmt != null)
stmt.executeUpdate(sql);
}
public void executeUpdate() throws SQLException {
if (