##################################数据库连接操作javabean:
public class DbTrans { public static Connection conn;
Statement stmt; /**
* 构造函数
*/
public DbTrans(){
initConnection();
} /**
* 带参数的构造函数
* @@param conn 连接
*/
public DbTrans(Connection conn){
this.conn = conn;
} /**
* 初始化建立连接
*/
private void initConnection(){
try{
if(conn == null||conn.isClosed()){
// DBConnectionManager connMgr=DBConnectionManager.getInstance();
// conn = connMgr.getConnection("sqlconn");
conn=DBConnectionManager.getConnection();
}
}
catch(Exception ex){
System.out.println("Can not get new Connection"+ex.getMessage());
}
} public PreparedStatement getPreparedStmt(String sql) throws SQLException{
PreparedStatement preStmt=null;
try {
preStmt = conn.prepareStatement(sql);
}
catch(SQLException ex){
ex.printStackTrace();
throw ex;
}
return preStmt;
}
/**
* executeQuery操作,用于数据查询,主要是Select
* @@param sql 查询字段
* @@return 数据集
* @@throws SQLException 捕捉错误
*/
public ResultSet executeQuery(String sql) throws SQLException {
ResultSet rs = null;
try {
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
}
catch (SQLException ex) {
ex.printStackTrace();
System.out.println("dbTrans.executeQuery:"+ex.getMessage());
throw ex;
}
return rs;
} /**
* executeUpdate操作,用于数据更新,主要是Update,Insert
* @@param sql 查询字段
* @@throws SQLException 捕捉错误
*/
public void executeUpdate(String sql) throws SQLException {
try {
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
stmt.executeUpdate(sql);
}
catch (SQLException ex) {
ex.printStackTrace();
System.out.println("dbTrans.executeUpdate:"+ex.getMessage());
throw ex;
}
} public int[] doBatch(String sql) throws SQLException {
int[] rowResult=null;
String a;
try{
stmt = conn.createStatement();
StringTokenizer st=new StringTokenizer(sql,";");
while (st.hasMoreElements()) {
a = st.nextToken();
stmt.addBatch(a);
}
rowResult=stmt.executeBatch();
}
catch (SQLException ex) {
ex.printStackTrace();
System.out.println("dbTrans.doBatch"+ex.getMessage());
throw ex;
}
return rowResult;
} /**
* 关闭对象
* @@throws SQLException 捕捉错误
*/
public void close() throws SQLException{
if(stmt != null) stmt.close();
if(conn != null&&!conn.isClosed()) conn.close();
}
}##################################javabean:
public class Post { String SQL = "";
int PostID;
DbTrans DBSQL;
ResultSet rs = null;
public Post() {
this.DBSQL = new DbTrans();
}
public String getBadWords(){
String BadWord="";
try {
SQL = "select * from badwords";
ResultSet rs = DBSQL.executeQuery(SQL);
while(rs.next()){
BadWord+=rs.getString("BadWord");
}
rs.close();
}
catch (SQLException e) {
}
return BadWord;
}
public void close() {
try {
DBSQL.close();
}
catch (SQLException e) {
}
}
}#########################jsp程序:
<%@ page language="java" contentType="text/html; charset=gb2312" %>
<jsp:useBean id="PostBean" scope="page" class="com......" /><%
out.print(PostBean.getBadWords());
%>
以上jsp程序用完后,怎么释放该数据库连接?
是不是在jsp程序的最后加入PostBean.close();
???
public class DbTrans { public static Connection conn;
Statement stmt; /**
* 构造函数
*/
public DbTrans(){
initConnection();
} /**
* 带参数的构造函数
* @@param conn 连接
*/
public DbTrans(Connection conn){
this.conn = conn;
} /**
* 初始化建立连接
*/
private void initConnection(){
try{
if(conn == null||conn.isClosed()){
// DBConnectionManager connMgr=DBConnectionManager.getInstance();
// conn = connMgr.getConnection("sqlconn");
conn=DBConnectionManager.getConnection();
}
}
catch(Exception ex){
System.out.println("Can not get new Connection"+ex.getMessage());
}
} public PreparedStatement getPreparedStmt(String sql) throws SQLException{
PreparedStatement preStmt=null;
try {
preStmt = conn.prepareStatement(sql);
}
catch(SQLException ex){
ex.printStackTrace();
throw ex;
}
return preStmt;
}
/**
* executeQuery操作,用于数据查询,主要是Select
* @@param sql 查询字段
* @@return 数据集
* @@throws SQLException 捕捉错误
*/
public ResultSet executeQuery(String sql) throws SQLException {
ResultSet rs = null;
try {
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
}
catch (SQLException ex) {
ex.printStackTrace();
System.out.println("dbTrans.executeQuery:"+ex.getMessage());
throw ex;
}
return rs;
} /**
* executeUpdate操作,用于数据更新,主要是Update,Insert
* @@param sql 查询字段
* @@throws SQLException 捕捉错误
*/
public void executeUpdate(String sql) throws SQLException {
try {
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
stmt.executeUpdate(sql);
}
catch (SQLException ex) {
ex.printStackTrace();
System.out.println("dbTrans.executeUpdate:"+ex.getMessage());
throw ex;
}
} public int[] doBatch(String sql) throws SQLException {
int[] rowResult=null;
String a;
try{
stmt = conn.createStatement();
StringTokenizer st=new StringTokenizer(sql,";");
while (st.hasMoreElements()) {
a = st.nextToken();
stmt.addBatch(a);
}
rowResult=stmt.executeBatch();
}
catch (SQLException ex) {
ex.printStackTrace();
System.out.println("dbTrans.doBatch"+ex.getMessage());
throw ex;
}
return rowResult;
} /**
* 关闭对象
* @@throws SQLException 捕捉错误
*/
public void close() throws SQLException{
if(stmt != null) stmt.close();
if(conn != null&&!conn.isClosed()) conn.close();
}
}##################################javabean:
public class Post { String SQL = "";
int PostID;
DbTrans DBSQL;
ResultSet rs = null;
public Post() {
this.DBSQL = new DbTrans();
}
public String getBadWords(){
String BadWord="";
try {
SQL = "select * from badwords";
ResultSet rs = DBSQL.executeQuery(SQL);
while(rs.next()){
BadWord+=rs.getString("BadWord");
}
rs.close();
}
catch (SQLException e) {
}
return BadWord;
}
public void close() {
try {
DBSQL.close();
}
catch (SQLException e) {
}
}
}#########################jsp程序:
<%@ page language="java" contentType="text/html; charset=gb2312" %>
<jsp:useBean id="PostBean" scope="page" class="com......" /><%
out.print(PostBean.getBadWords());
%>
以上jsp程序用完后,怎么释放该数据库连接?
是不是在jsp程序的最后加入PostBean.close();
???
解决方案 »
- hibernate3中many-to-one的问题,急
- 请路过的英雄看下,关于JSTL的问题
- jfreechar使用中两个问题
- csdn的网站是不是很烂!
- 请高手指教这是什么错误 description The server encountered an internal error () that prevented it from fulfilling this request.
- 网页树型按钮制作问题?
- 菜鸟又来求助了!
- 请问大家Eclipse2.1这个工具怎么用呀?哪里才有全部的插件下载呀?
- 一点都不懂JAVA的人提问!!!!
- 大家讨论一下jsp的web服务器啊??
- 如何用servlet在页面上显示记录
- 疑问
你说的:
以上jsp程序用完后,怎么释放该数据库连接?
是不是在jsp程序的最后加入PostBean.close();
这个问题,我觉得应该这样,数据库的释放应该在javabean或者ejb里面进行,最好不要在jsp页面进行。只要在你数据库操作完后关闭数据库连接就可以了,我们一般是这样操作的。
try
{
数据库操作
}
catch(SQLException e)
{
异常处理
}
finally
{
closeDB(conn,pst,rs);
} public static void closeDB(Connection conn,Statement stmt) throws SystemException
{
try {
if(stmt != null) stmt.close();
if (conn != null) conn.close();
} catch (SQLException e) {
SystemException lmsse = new SystemException(e);
lmsse.setErrorPage(Err_Constants.DATABASE_FAIL);
throw lmsse;
}
}
public static void closeDB(Connection conn,Statement stmt,ResultSet rs) throws SystemException
{
try {
if(rs !=null) rs.close();
if(stmt != null) stmt.close();
if (conn != null) conn.close();
} catch (SQLException e) {
SystemException lmsse = new SystemException(e);
lmsse.setErrorPage(Err_Constants.DATABASE_FAIL);
throw lmsse;
}
}
{
try {
if(rs !=null) rs.close();
if(stmt != null) stmt.close();
if (conn != null) conn.close();
} catch (SQLException e) {
SystemException lmsse = new SystemException(e);
lmsse.setErrorPage(Err_Constants.DATABASE_FAIL);
throw lmsse;
}
}
只要在每次你从连接此获得connection之后,在进行完操作的时候记得conn。close()就是。当然在这之前还把其他的statement和resultset关闭
这就算释放了,conn关闭连接,连接池也就知道该conn是free的good luck
这一步是不用热外编程的。但是如果连接池的代码是自己写的话,仅仅close是不够的。还必须将你对应的放置connection的useConnections这个hashtable里面的该conn进行remove。