做限制客户端连接数据库次数的程序,出了点问题,请麻烦看一下,代码比较长(50分)

  • 主题发起人 主题发起人 ★蓝天★
  • 开始时间 开始时间

★蓝天★

Unregistered / Unconfirmed
GUEST, unregistred user!
我做了一个类SQLBridge,是对数据库进行操作的各功能,然后,为了限制客户端
连接数据库的次数,我又做了个ConnPool。在代码中,我设了限制三条数据库连接
import java.util.*;
import java.sql.*;
public class ConnPool
{
private static final int defaultMaxConnections=3;
private Vector freeConnections;
private Hashtable boundConnections;
private String driverName;
private String jdbcURL;
private String userName;
private String passWord;
private int maxConnections;
public ConnPool(int numConnections)
{
maxConnections=numConnections;
boundConnections=null;
freeConnections=null;
driverName="";
jdbcURL="";
userName="";
passWord="";
}
public ConnPool()
{
this(defaultMaxConnections);
}
public void closeDB() throws SQLException
{
if(boundConnections!=null)
{
for(Enumeration e=boundConnections.elements();e.hasMoreElements();)
{
Connection conn=(Connection)e.nextElement();
conn.close();
}
boundConnections.clear();
boundConnections=null;
}
if(freeConnections!=null)
{
for(Enumeration e=freeConnections.elements();e.hasMoreElements();)
{
Connection conn=(Connection)e.nextElement();
conn.close();
}
freeConnections.removeAllElements();
freeConnections=null;
}
}
public synchronized Connection getConnection() throws SQLException
{
if(freeConnections==null) throw new SQLException("The conection pool has not been established yet.");
if(boundConnections.get( Thread.currentThread() ) !=null) throw new SQLException("Cannot get connections over once for this current running thread .");
try
{
if(freeConnections.size()==0) wait();
}
catch(InterruptedException ex)
{
throw new SQLException(ex.toString());
}
Connection conn=(Connection)freeConnections.firstElement();
freeConnections.removeElement(conn);
boundConnections.put(Thread.currentThread(),conn);
return conn;
}
public void openDB(String drvName,String url,String uname,String passwd) throws SQLException
{
try
{
boundConnections=new Hashtable(maxConnections);
freeConnections=new Vector(maxConnections);
Class.forName(drvName);
for(int i=0;i<maxConnections;i++)
freeConnections.addElement(DriverManager.getConnection(url,uname,passwd));
}
catch(Exception ex)
{
boundConnections=null;
freeConnections=null;
throw new SQLException(ex.toString());
}
}
public synchronized void returnConnection() throws SQLException
{
Connection conn=(Connection)boundConnections.remove(Thread.currentThread());
if(conn==null) throw new SQLException("The connection which this current running thread got is not found.");
freeConnections.addElement(conn);
notify();
}
public void setConnectionSwitch(String onoff) throws Exception
{
try
{
if(onoff.equalsIgnoreCase("ON"))
openDB(driverName,jdbcURL,userName,passWord);
else
if(onoff.equalsIgnoreCase("OFF"))
closeDB();
}
catch(SQLException ex)
{
throw new Exception(ex.toString());
}
}
public void setMaxConnections(int numConnections)
{
maxConnections=numConnections;
}
public void setDriverName(String drvName)
{
driverName=drvName;
}
public void setJdbcURL(String url)
{
jdbcURL=url;
}
public void setUserName(String uname)
{
userName=uname;
}
public void setPassWord(String passwd)
{
passWord=passwd;
}
}
import java.sql.*;
import ConnPool;
public class SQLBridge
{
private ConnPool connPool;
private Connection conn;
private ResultSet rs;
private ResultSetMetaData rsmd;
private Statement stmt;
private String driverName;
private String jdbcURL;
private String userName;
private String passWord;
public SQLBridge() //构造函数
{
connPool=null;
conn=null;
rs=null;
rsmd=null;
stmt=null;
}
public void setDriverName(String drvname)
{
driverName=drvname;
}
public void setJdbcURL(String url)
{
jdbcURL=url;
}
public void setUserName(String uname)
{
userName=uname;
}
public void setPassWord(String pass)
{
passWord=pass;
}
public String getDriverName()
{
return driverName;
}
//连接数据库
public void openDB(String drvname,String url,String uname,String passwd) throws Exception
{
if(conn!=null&amp;&amp;!conn.isClosed())
throw new Exception ("the connection has been established already.");
clearResult();
try
{
Class.forName(drvname);
}
catch(Exception e)
{
throw new Exception("failure");
}
conn=DriverManager.getConnection(url,uname,passwd);
}
public void openDB(ConnPool pool) throws SQLException
{
if(conn!=null&amp;&amp;!conn.isClosed()) throw new SQLException("The connection has been established already.");
if(pool==null) throw new SQLException("The connection pool cannot be found.");
clearResult();
connPool=pool;
conn=connPool.getConnection();
}
//关闭数据库连接
public void closeDB() throws SQLException
{
clearResult();
if(connPool!=null)
{
connPool.returnConnection();
connPool=null;
}
if(conn==null)
throw new SQLException("This connection has been already closed");
if(conn.isClosed()) throw new SQLException("This connection has been closed.");
conn.close();
conn=null;
}
//设置数据库的开关
public void setConnectionSwitch(String onoff) throws Exception
{
try
{
if (onoff.equalsIgnoreCase("ON"))
{
if(connPool==null)
openDB(driverName,jdbcURL,userName,passWord);
else
openDB(connPool);
}
else
if(onoff.equalsIgnoreCase("OFF"))
{
closeDB();
}
}
catch(Exception e)
{
throw new Exception("connection failure");
}
}
//执行sql语句;
public int execSQL(String sqlstmt) throws Exception
{
if(conn==null||conn.isClosed()) throw new Exception("the connection has not been established yes.");
if(sqlstmt==null) throw new Exception("sql_stmt is null");
clearResult();
conn.setAutoCommit(true);
stmt=conn.createStatement();
if(sqlstmt.toUpperCase().startsWith("SELECT"))
{
rs=stmt.executeQuery(sqlstmt);
rsmd=rs.getMetaData();
return -1;
}
else
{
int numrow=stmt.executeUpdate(sqlstmt);
clearResult();
return numrow;
}
}
//清空数据源
public void clearResult() throws SQLException
{
if(rs!=null) rs.close();
rs=null;
if(stmt!=null) stmt.close();
stmt=null;
rsmd=null;
}
//执行多条sql语句
public void execUpdate(String[] sqlstmts) throws SQLException
{
if(conn==null||conn.isClosed()) throw new SQLException("The connection has not been established yes .");
if(sqlstmts==null||sqlstmts.length==0) throw new SQLException("SQL-statement is null.");
clearResult();
conn.setAutoCommit(false);
try
{
for(int i=0;i<sqlstmts.length;i++)
{
stmt=conn.createStatement();
stmt.executeUpdate(sqlstmts);
stmt.close();
}
conn.commit();
}
catch(SQLException ex)
{
conn.rollback();
throw ex;
}
}
//得到字段名
public String[] getColumnNames() throws SQLException
{
if(rsmd==null)
throw new SQLException("ResultSet is null");
String[] columnnames=new String[getColumnCount()];
for(int i=1;i<=columnnames.length;i++)
columnnames[i-1]=rsmd.getColumnName(i);
return columnnames;
}
//得到字段总数
public int getColumnCount() throws SQLException
{
if(rsmd==null)
throw new SQLException("ResultSet is null.");
return rsmd.getColumnCount();
}
//光标指向下一条
public boolean nextRow() throws SQLException
{
if(rs==null)
throw new SQLException("ResultSet is null.");
return rs.next();
}
protected Object getField(int column,boolean convertToString) throws SQLException
{
if(rs==null||rsmd==null)
throw new SQLException("ResultSet is null");
switch(rsmd.getColumnType(column))
{
case Types.BIGINT:
if(convertToString)
return String.valueOf(rs.getLong(column));
else
return new Long(rs.getLong(column));
case Types.BINARY:
if(convertToString)
return Byte.toString(rs.getByte(column));
else
return new Byte(rs.getByte(column));
case Types.BIT:
if(convertToString)
return String.valueOf(rs.getBoolean(column));
else
return new Boolean(rs.getBoolean(column));
case Types.CHAR:
return rs.getString(column);
case Types.DATE:
if(convertToString)
return(rs.getDate(column)).toString();
else
return rs.getDate(column);
case Types.DECIMAL:
if(convertToString)
return (rs.getBigDecimal(column,rsmd.getScale(column))).toString();
else
return rs.getBigDecimal(column,rsmd.getScale(column));
case Types.DOUBLE:
if(convertToString)
return String.valueOf(rs.getDouble(column));
else
return newdo
uble(rs.getDouble(column));
case Types.FLOAT:
if(convertToString)
return String.valueOf(rs.getDouble(column));
else
return new Float(rs.getDouble(column));
case Types.INTEGER:
if(convertToString)
return String.valueOf(rs.getInt(column));
else
return new Integer(rs.getInt(column));
case Types.LONGVARBINARY:
if(convertToString)
return (rs.getBinaryStream(column)).toString();
else
return rs.getBinaryStream(column);
case Types.LONGVARCHAR:
return rs.getString(column);
case Types.NULL:
if(convertToString)
return "NULL";
else
return null;
case Types.NUMERIC:
if(convertToString)
return(rs.getBigDecimal(column,rsmd.getScale(column))).toString();
else
return rs.getBigDecimal(column,rsmd.getScale(column));
case Types.REAL:
if(convertToString)
return String.valueOf(rs.getFloat(column));
else
return new Float(rs.getFloat(column));
case Types.SMALLINT:
if(convertToString)
return String.valueOf(rs.getShort(column));
else
return new Short(rs.getShort(column));
case Types.TIME:
if(convertToString)
return (rs.getTime(column)).toString();
else
return rs.getTime(column);
case Types.TIMESTAMP:
if(convertToString)
return (rs.getTimestamp(column)).toString();
else
return rs.getTimestamp(column);
case Types.TINYINT:
if(convertToString)
return String.valueOf(rs.getByte(column));
else
return new Byte(rs.getByte(column));
case Types.VARCHAR:
return rs.getString(column);
default:
if(convertToString)
return(rs.getObject(column)).toString();
else
return rs.getObject(column);
}
}
//根据字段序号,得到字段值
public Object getField(int column) throws SQLException
{
return getField(column,false);
}
//根据字段名,得到字段值
public Object getField(String fieldName) throws SQLException
{
return getField(rs.findColumn(fieldName),false);
}
//根据字段序号,得到字段值(字符型)
public String getFieldString(int column) throws SQLException
{
return (String)getField(column,true);
}
//根据字段名,得到字段值(字符型)
public String getFieldString(String fieldName) throws SQLException
{
return (String)getField(rs.findColumn(fieldName),true);
}
public void setConnPool(ConnPool pool)
{
connPool=pool;
}
}

执行jsp文件:
<%@ page language="java" import="SQLBridge,ConnPool;" contentType="text/html;charset=gb2312"%>
<jsp:useBean id="pool" class="ConnPool" scope="application" >
<jsp:setProperty name="pool" property="driverName" value="com.microsoft.jdbc.sqlserver.SQLServerDriver" />
<jsp:setProperty name="pool" property="jdbcURL" value="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=bookstoreDB" />
<jsp:setProperty name="pool" property="userName" value="sa" />
<jsp:setProperty name="pool" property="passWord" value="" />
<jsp:setProperty name="pool" property="connectionSwitch" value="on" />
</jsp:useBean>
<jsp:useBean id="sql" class="SQLBridge" scope="page">
<jsp:setProperty name="sql" property="connPool" value="<%=pool%>"/>
<jsp:setProperty name="sql" property="connectionSwitch" value="on"/>
</jsp:useBean>
<html>
<head>
<title>图书订购网</title>
<meta http-equiv="Content-Type" content="text/html;
charset=gb2312">
</head>
<body bgcolor="#CCCCCC" >
<form name="indexForm" method="post" action="bookOrder.jsp">
<table width="25%" border="1" align="center" cellpadding="5" bordercolor="#999999" bgcolor="#CCCCFF">
<tr>
<td><font size="6" face="楷体_GB2312"><strong>图书订购网</strong></font></td>
</tr>
</table>
<p>&amp;nbsp;</p>
<table border="0" align="center">
<tr>
<td >帐号: </td>
<td ><input type="text" name="id" size="15"></td>
<td >&amp;nbsp;</td>
</tr>
<tr>
<td>密码: </td>
<td><input type="text" name="password" size="15"></td>
<td><input type="submit" name="Submit" value=" 订 购 "></td>
</tr>
</table>
<p align="center"><a href="memberRegister.htm"><strong>加入会员</strong></a></p>
</form>
</body>
</html>
<%sql.closeDB();
%>

按理说,我在页面最后,执行了sql.closeDB();,那就应该可以打开此相同的页面无数个,但是现在只能打
开三个页面,当打开第四个页面时,就出错。提示conn处于关闭状态,这问题应该如何解决?








 
后退
顶部