·您的位置: 首页 » 资源教程 » 编程开发 » JAVA、JSP » JSP用连接池连数据库的问题

JSP用连接池连数据库的问题

类别: JSP教程  评论数:0 总得分:0
各位高手麻烦帮我看下面这三个文件(一个jsp,两个.java),我在IE输入地址时,提示如下错误(代码应该没有错,不知是不是设置的问题),望高手指教,不胜感激:

D:Program FilesApache GroupTomcat 4.1workStandalonelocalhost_shoppingindex_jsp.java:44: package mybean does not exist
mybean.DBConnManager connManager = null;
^



An error occurred at line: 2 in the jsp file: /shopping/index.jsp

Generated servlet error:
D:Program FilesApache GroupTomcat 4.1workStandalonelocalhost_shoppingindex_jsp.java:46: package mybean does not exist
connManager = (mybean.DBConnManager) pageContext.getAttribute("connManager", PageContext.APPLICATION_SCOPE);
^



An error occurred at line: 2 in the jsp file: /shopping/index.jsp

Generated servlet error:
D:Program FilesApache GroupTomcat 4.1workStandalonelocalhost_shoppingindex_jsp.java:49: package mybean does not exist
connManager = (mybean.DBConnManager) java.beans.Beans.instantiate(this.getClass().getClassLoader(), "mybean.DBConnManager");
^


//下面这两个是连接池,下面两个文件都放在一个mybean的包里.

package mybean;
import java.sql.*;
import java.util.*;

public class DBConnPool {
//正在使用连接的数量
private int using;
//目前可用的连接数,即空闲连接
private Vector connections=new Vector();
//最大连接数
private int maxconn;
//连接池名
private String poolname;
//数据库标识
private String dbid;
//驱动程序名
private String drivername;
//数据库帐号
private String username;
//数据库密码
private String passwd;

public DBConnPool(String poolname,String dbid,String drivername,
String username,String passwd,int maxconn){
this.poolname=poolname;
this.drivername=drivername;
this.dbid =dbid;
this.username =username;
this.passwd=passwd;
this.maxconn=maxconn;
}

/*将空闲连接返回给连接池*/
public synchronized void returnConnection(Connection conn){
//将指定连接加到向量末尾
connections.addElement(conn);
//连接用户减一
using--;
}

/*从连接池得到一个连接*/
public synchronized Connection getConnection(){
Connection conn = null; //Connection是一个类,
//connections是一个向量,用于存储连接对象,它所存储是的所有空闲状态的可用连接
if (connections.size() > 0) {

//获取连接列表的第一个连接
conn = (Connection) connections.elementAt(0);
connections.removeElementAt(0);//获得一个连接,并将此连接从队列中删除.

//如果此连接已关闭,刚继续获取,
try {
if (conn.isClosed())
conn = getConnection();
}
catch (Exception e) {
e.printStackTrace();
}
}
//如果实际使用的连接数小于最大连接数即有可用连接),就新增加一个连接
else if (maxconn == 0 || using < maxconn){
//如此时无可用连接(maxconn == 0)且连接数又未达到上限(using < maxconn)),就创建一个新连接
conn=newConnection();
}
//如果连接数已达到上限就返回空指针
if (conn!=null){
using++;
}
return conn;
}

/*创建新的连接*/
public Connection newConnection(){
Connection conn=null;
try{
//加载驱动
Class.forName(drivername);
conn=DriverManager.getConnection(dbid,username,passwd);
}catch(Exception e){
e.printStackTrace();
return null;
}
return conn;
}

/*关闭所有连接*/
public synchronized void closeConn(){
Enumeration allConnections=connections.elements();
while (allConnections.hasMoreElements()){
Connection conn=(Connection) allConnections.nextElement();
try{
conn.close();
}catch(SQLException e){
e.printStackTrace();
}
}
connections.removeAllElements();
}
}




package mybean;
import java.sql.*;
import java.util.*;

public class DBConnManager {
//连接池名列表
private Vector poolnames =new Vector();
//驱动程序名列表
private Vector drivernames=new Vector();
//数据库标识列表
private Vector dbids=new Vector();
//用户名列表
private Vector usernames=new Vector();
//用户密码列表
private Vector passwds=new Vector();
//最大连接数列表
private Vector maxconns=new Vector();
//连接池队列
private Hashtable connPools=new Hashtable();

public DBConnManager() {
//添加Access数据库的连接信息
poolnames.addElement("access");
drivernames.addElement("sun.jdbc.odbc.JdbcOdbcDriver");
dbids.addElement("jdbc:odbc:shopping");
usernames.addElement("");
passwds.addElement("");
maxconns.addElement("5");

//添加SQL Server2000数据库的连接信息
poolnames.addElement("sqlserver2000");
drivernames.addElement("com.microsoft.jdbc.sqlserver.SQLServerDriver");
dbids.addElement("jdbc:microsoft:sqlserver://localhost:1433;DatabserName=shopping");
usernames.addElement("");
passwds.addElement("");
maxconns.addElement("5");

//连接Mysql数据库信息
poolnames.addElement("mysql");
drivernames.addElement("org.gjt.mm.mysql.Driver");
dbids.addElement("jdbc:mysql://localhost/shopping");
usernames.addElement("");
passwds.addElement("");
maxconns.addElement("5");

//连接Oracle8i/9i数据库
poolnames.addElement("oracle");
drivernames.addElement("oracle.jdbc.driver.OracleDriver");
dbids.addElement("jdbc:oracle:thin:@localhost:1521:shopping");
usernames.addElement("");
passwds.addElement("");
maxconns.addElement("5");


//创建连接池
createPools();
}

/*将连接返回给由指定的连接池*/
public void returnConnection(String name,Connection conn){
DBConnPool pool=(DBConnPool) connPools.get(name);
if (pool!=null)
{
pool.returnConnection(conn);
}
}

/*得到一个指定连接池中的连接*/
public Connection getConnection(String name){
DBConnPool pool=(DBConnPool) connPools.get(name);
if (pool!=null)
{
return pool.getConnection();
}
return null;
}

/*关闭所有连接*/
public synchronized void closeConns(){
Enumeration allPools=connPools.elements();
while (allPools.hasMoreElements()){
DBConnPool pool=(DBConnPool) allPools.nextElement();
pool.closeConn();
}
}

/*创建连接池*/
private void createPools(){
for (int i=0;i<poolnames.size();i++)
{
String poolname=poolnames.elementAt(i).toString();
String drivername=drivernames.elementAt(i).toString();
String dbid=dbids.elementAt(i).toString();
String username=usernames.elementAt(i).toString();
String passwd=passwds.elementAt(i).toString();
int maxconn=0;
try{
maxconn=Integer.parseInt(maxconns.elementAt(i).toString());
}catch (NumberFormatException e){
e.printStackTrace();
}
DBConnPool pool=new DBConnPool(poolname,drivername,dbid,username,
passwd,maxconn);
connPools.put(poolname,pool);
}
}
}


下面这个是主页面.JSP文件

<%@ page contentType="text/html; charset=GBK" %>
<%@ page import ="java.sql.*" %>
<jsp:useBean id="connManager" scope="application" class="mybean.DBConnManager" />
<html>
<head>
<title>
购物商城首页
</title>
</head>
<body bgcolor="#B0C4DE">
<center><h1>欢迎访问本购物商城</h1></center>
<%
//Connection connA=connManager.getConnection("access");
Connection connS=connManager.getConnection("sqlserver2000");
if (connS==null)
{
%>
数据库正忙,请稍后再访问
<%
}
//Statement stmtA=connA.createStatement();
Statement stmtS=connS.createStatement();
%>

<%
String sql="select * from userinfo";
ResultSet rs=stmtS.executeQuery(sql);
while (rs.next())
{
%>
<tr>
<td><%=rs.getString("username")%></td>
<td><%=rs.getString("userpass")%></td>
</tr>
<%
}
rs.close();
stmtS.close();
connManager.returnConnection("sqlserver2000",connS);
%>

</body>
</html>
-= 资 源 教 程 =-
文 章 搜 索
关键词:
类型:
范围:
纯粹空间 softpure.com
Copyright © 2006-2008 暖阳制作 版权所有
QQ: 15242663 (拒绝闲聊)  Email: faisun@sina.com
 纯粹空间 - 韩国酷站|酷站欣赏|教程大全|资源下载|免费博客|美女壁纸|设计素材|技术论坛   Valid XHTML 1.0 Transitional
百度搜索 谷歌搜索 Alexa搜索 | 粤ICP备19116064号-1