程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> JSP使用連接池連接數據庫的問題

JSP使用連接池連接數據庫的問題

編輯:關於JAVA

各位高手麻煩幫我看下面這三個文件(一個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

{

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文件

購物商城首頁

歡迎訪問本購物商城

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved