重复刷新对我的连接池有影响吗?请高手指点我的连接池代码

yzf111 2004-09-23 12:48:53
如题,请各位大侠指教!
***************************************************
DBConnect.java
**************************************************
package web.common;
import javax.naming.*;
import javax.sql.*;
import java.sql.*;
import java.io.*;
import java.util.*;
public class DBConnect {
static private Connection Connection1=null; //数据库连接
private java.sql.Statement stmt = null;

public DBConnect() {
try{
Context ctx = new InitialContext();
DataSource ds =(DataSource)ctx.lookup("java:comp/env/jdbc/mysqlDB");
if (ds != null) {
Connection1= ds.getConnection();
}
}catch(Exception e1) {
e1.printStackTrace();
}
}

public Connection getConn(){
try
{
if((Connection1==null) || (Connection1.isClosed()))
{
new DBConnect();
}
}
catch( Exception ex )
{
System.err.println("Error in Query - SQLBean : ");
ex.printStackTrace(System.err);
}
return Connection1;
}

/**
* 用于查询的SQL语句
* @param SQL SQL语句字串 如:select * from tablename
* @return ResultSet 记录集
*/
public ResultSet Query(String SQL)
{

ResultSet rs = null;
try
{
if((Connection1==null) || (Connection1.isClosed()))
{
new DBConnect();
}
stmt = Connection1.createStatement(java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,java.sql.ResultSet.CONCUR_READ_ONLY);
rs = stmt.executeQuery(SQL);
}
catch( Exception ex )
{
System.err.println("Error in Query - SQLBean : ");
ex.printStackTrace(System.err);
}
return rs;
}


/**
* 用于查询的SQL语句结果集记录数
* @param SQL SQL语句字串 如:select count(*) from tablename
* @int 记录集
*/
public int Querycount(String SQL)
{
ResultSet rs1 = null;
int number=0;
try
{
if((Connection1==null) || (Connection1.isClosed()))
{
new DBConnect();
}
stmt = Connection1.createStatement() ;
rs1 = stmt.executeQuery(SQL);
rs1.next();
number=rs1.getInt(1);
rs1.close();
stmt.close();
//

}
catch( Exception ex )
{
System.err.println("Error in Query - SQLBean : ");
ex.printStackTrace(System.err);
}

return number;
}

/**
* 执行SQL语句,返回结果(ture,false)
* @param SQL
* @return boolean
*/

public boolean execute(String SQL)
{
boolean exeresult=false;
try
{
if((Connection1==null) || (Connection1.isClosed()))
{
new DBConnect();
}
stmt = Connection1.createStatement();
// Connection1.createStatement(ResultSet.,ResultSet.CONCUR_READ_ONLY );
stmt.execute(SQL);
exeresult=true;
}
catch( Exception ex )
{
System.err.println("Error in execute - SQL : ");
ex.printStackTrace(System.err);
}
return exeresult;
}
/**
* 关闭连接
*/

public void closeconn()
{
try
{
if(stmt!=null)
{
stmt.close();
}
if(Connection1!=null)
{
Connection1.close();
}
/* if(ctx!=null)
{
ctx.close();
}
*/
}
catch(Exception e)
{
System.out.println("error is "+e.getMessage());
}
}
}
***************************************************
jsp页面调用
**************************************************
...
...
DBConnect dbc = new DBConnect();
try{
}catche(Exception ec){
out.println(ec.getMessage());
}finally{
dbc.closeconn();
}
...
...
...全文
277 18 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
yzf111 2004-09-29
  • 打赏
  • 举报
回复
有点郁闷,结了
pcdll 2004-09-24
  • 打赏
  • 举报
回复
IceCraft(心淡情浓)
用jsp直连会造成数据库死锁这是不争的事实,而写在bean里面,就完全避免了.别跟我说什么别的.有的东西就是没办法解释的,所以我只能那样解释了.


to 楼主:
你的数据库访问虽然写在了bean里面,但你对数据库的具体操作还是写在了jsp上.一般来说,jsp仅仅只是表现页面的.比如你要读出一些数据,在页面循环出来,操作应该是在bean里面读得这些记录,组成一个Iterator,然后在页面迭代出来.
yzf111 2004-09-24
  • 打赏
  • 举报
回复
to csdn6644()
这个不是要不要多线程的问题
你网站同一时间就一个人访问么?
单线程并不代表同一时间只有一个人能访问。
private java.sql.Statement stmt = null;
------
这个也没必要的
你说的对,这个是没有必要。

希望大家再提一些意见。谢谢
yzf111 2004-09-23
  • 打赏
  • 举报
回复
上面修改一下
***************************************************
jsp页面调用
**************************************************
...
...
DBConnect dbc = new DBConnect();
try{
ResultSet rs = dbc.Query("select * from form");
while(!rs.next()){
out.println(rs.getString(2));
}
}catche(Exception ec){
out.println(ec.getMessage());
}finally{
rs.close();
dbc.closeconn();
}
...
...
smartheye 2004-09-23
  • 打赏
  • 举报
回复
这个不是要不要多线程的问题
你网站同一时间就一个人访问么?

private java.sql.Statement stmt = null;
------
这个也没必要的
yzf111 2004-09-23
  • 打赏
  • 举报
回复
暂时网站不用多线程,请用单线程考虑好了,还望高手再给出一些指证。
smartheye 2004-09-23
  • 打赏
  • 举报
回复
public Connection getConn(){
try
{
if((Connection1==null) || (Connection1.isClosed()))
{
new DBConnect();
}
}
catch( Exception ex )
{
System.err.println("Error in Query - SQLBean : ");
ex.printStackTrace(System.err);
}
return Connection1;
}


-----------------------
假设有thread a,b,同时去执行getConn
假设此时Connection1=null
a执行if((Connection1==null) 尚未执行new DBConnect()时
b也执行if((Connection1==null),那么也可以通过,去执行new DBConnect()
所以就会丢失连接+一个context

总之不太理想,倒还不如像楼上写一个单纯的wrapper类
dededidedu 2004-09-23
  • 打赏
  • 举报
回复
学习ing
SS_ 2004-09-23
  • 打赏
  • 举报
回复
支持帮主
SS_ 2004-09-23
  • 打赏
  • 举报
回复
大家有没有写好的连接类,拿出来讨论一下,做个比较,好学习,我先来一个初学者用的。。。



import java.sql.*;
import java.util.*;
import com.microsoft.jdbcx.sqlserver.SQLServerDataSource;
/**
* @author Biggie
*
* Class Function:数据库连接bean,取sql server JDBC数据源,这个数据源也可以用
* Tomcat,WebLogic,resin等提供
* 支持各种容器及平台
* @version 1.0A
* */
public class DBConnect {
file://You Host IP
private String strHostAddress="127.0.0.1";
file://Host Port
private int intHostPort=1433;
file://UserName
private String strUserName="sa";
file://PassWord
private String strPassWord="";
file://DataBase Name
private String strDataName="dac";
file://Max Connection
private int intMaxConnection=10;
private Connection con=null;
private Statement stmt=null;
private ResultSet rs=null;
file://JDBC source
private SQLServerDataSource source=null;
ArrayList ArrayRs=new ArrayList();
/**
* @param 构造函数注册JDBC驱动程序
* */
public DBConnect(){
try{
if(source==null){
source=new SQLServerDataSource();
source.setDatabaseName(strDataName);
source.setServerName(strHostAddress);
source.setPortNumber(intHostPort);
source.setUser(strUserName);
source.setPassword(strPassWord);
file://source.setHostProcess(intMaxConnection);

}
}catch(Exception e){
System.out.println("open database error:"+e.getMessage());
}
}
/**
* @param executeQuery查询数据库方法
* @param 每条ArrayList记录存为String[] 数组
* @return ArrayList
* @exception SQLException
*/
public ArrayList executeQuery(String strSql) throws SQLException {
rs=null;
try{
con=source.getConnection();
stmt=con.createStatement();
rs=stmt.executeQuery(strSql);

ResultSetMetaData rsmd=rs.getMetaData();
int numberOfColumns = rsmd.getColumnCount();

file://判断是否为空
if(!ArrayRs.isEmpty()){
ArrayRs.clear();
}
/*
* 将每条记录写入数组
* 将数组放在ArrayList里
*/
while(rs.next()){
String[] strArrayTemp=new String[numberOfColumns];
for(int i=0;i<numberOfColumns;i++){
if(rs.getObject(i+1)==null){
strArrayTemp[i]= "";
}else{
strArrayTemp[i]=rs.getObject(i+1).toString();
}
}
ArrayRs.add(strArrayTemp);
}
return (ArrayList)ArrayRs.clone();
}catch(Exception e){
System.out.println("query error:" + e.getMessage());
}finally{
if (stmt != null) {
stmt.close();
}
if (con != null) {
con.close();
}
}
return ArrayRs;
}
/**
* @param executeInsert插入数据方法
* @return 插入条数是否成功(boolean)
*/
public boolean executeInsert(String strSql) throws SQLException{
rs=null;
try{
con=source.getConnection();
stmt=con.createStatement();

con.setAutoCommit(true);
int i=stmt.executeUpdate(strSql);

if(i==1){
return (true);
}
}catch(Exception e){
System.out.println("Insert error:"+e.getMessage());
}finally{
if (stmt != null) {
stmt.close();
}
if (con != null) {
con.close();
}
}
return (false);
}
/**
* @param executeUpdate修改数据方法
* @return 修改数据数(int)
*/
public int executeUpdate(String strSql) throws SQLException{
rs=null;
int j=0;
try{
con=source.getConnection();
stmt=con.createStatement();
con.setAutoCommit(false);

j=stmt.executeUpdate(strSql);
if(j>0){
con.commit();
}else{
con.rollback();
}
}catch(Exception e){
System.out.println("update error:"+e.getMessage());
}finally{
if (stmt != null) {
stmt.close();
}
if (con != null) {
con.close();
}
}
return j;
}
/**
* @param executeDelete删除数据方法
* @return 删除数据数(int)
*/
public int executeDelete(String strSql) throws SQLException{
rs=null;
int j=0;
try{
con=source.getConnection();
stmt=con.createStatement();

con.setAutoCommit(false);
j=stmt.executeUpdate(strSql);

if(j>0){
con.commit();
}else{
con.rollback();
}
}catch(Exception e){
System.out.println("Delete error:"+e.getMessage());
}finally{
if (stmt != null) {
stmt.close();
}
if (con != null) {
con.close();
}
}
return j;
}
}


SS_ 2004-09-23
  • 打赏
  • 举报
回复
思考中。。。。。。。。。。。
yzf111 2004-09-23
  • 打赏
  • 举报
回复
自己再顶
yzf111 2004-09-23
  • 打赏
  • 举报
回复
谢谢楼上各位的回复
...
DBConnect dbc = new DBConnect();
try{
ResultSet rs = dbc.Query("select * from form");
此部份改成
...
...
DBConnect dbc = new DBConnect();
ResultSet rs = null;
try{
rs = dbc.Query("select * from form");
就不会有问题了,其实我最主要是想让知道大家对我这个连接类的调用和释放提供一些介意。
都有分,请继续。。。

dkmilk 2004-09-23
  • 打赏
  • 举报
回复
我用dbcp测试都有问题。最后改成c3p0了
yzf111 2004-09-23
  • 打赏
  • 举报
回复
不好意思,表达错了,”JSP调用“改成“JAVABEAN调用”。事实上我都写在JAVABEAN里的
IceCraft 2004-09-23
  • 打赏
  • 举报
回复
pcdll(.net)的说法是非常错误的!
jsp文件首先在服务器端编译成了一个servlet。
用户请求一个Jsp页面时只是发送了一个请求给服务器,服务器执行这个servlet产生输出流返回给用户。
所以,这个servlet是被执行完了的,数据库连接自然也被正常关闭了。
用户在页面打开一半就关闭了页面,实际上只是接收了一部分服务器的返回信息,与服务器执行servlet一点关系也没有。
建议对jsp的执行原理再了解一些吧!
pcdll 2004-09-23
  • 打赏
  • 举报
回复
jsp里不应该出现数据库连接.
因为jsp并不可靠,如果别人在访问你的页面,开到一半,就关闭了,虽然你有finally,但可能会隔很长时间才会关闭.写在javabean里吧.
重复刷新肯定是有影响的,特别是写在jsp中的时候,你可以做可测试,按住F5
yzf111 2004-09-23
  • 打赏
  • 举报
回复
不是都睡觉去了吧,自己顶一下

81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧