JSP使用串連池串連資料庫的問題

來源:互聯網
上載者:User
js|串連資料庫|問題 各位高手麻煩幫我看下面這三個檔案(一個jsp,兩個.java),我在IE輸入地址時,提示如下錯誤(代碼應該沒有錯,不知是不是設定的問題),望高手指教,不勝感激:

D:\Program Files\Apache Group\Tomcat 4.1\work\Standalone\localhost\_\shopping\index_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 Files\Apache Group\Tomcat 4.1\work\Standalone\localhost\_\shopping\index_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 Files\Apache Group\Tomcat 4.1\work\Standalone\localhost\_\shopping\index_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();
}
//如果串連數已達到上限就返回null 指標
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>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.