Problems with JSP using connection pooling to connect to a database

Source: Internet
Author: User
Tags connection pooling connect odbc mysql tostring access database mysql database tomcat
js| Connection Database | Problem You can help me look at the following three files (a JSP, two. java), I entered the address in IE, prompted the following error (code should not be wrong, do not know if the problem is set up), look at the high finger teach, appreciate:

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 ");
^
The following two are connection pools, the following two files are placed in a mybean bag.

Package Mybean;
Import java.sql.*;
Import java.util.*;

public class Dbconnpool {
Number of connections being used
private int using;
Current number of connections available, that is, idle connections
Private vector connections=new vector ();
Maximum number of connections
private int maxconn;
Connection Pool Name
Private String poolname;
Database identification
Private String dbid;
Driver name
Private String drivername;
Database account number
Private String username;
Database Password
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;
}

/* Return the idle connection to the connection pool.
Public synchronized void ReturnConnection (Connection conn) {
Adds the specified connection to the end of the vector
Connections.addelement (conn);
Connect user minus One
using--;
}

/* Get a connection from the connection pool * *
Public synchronized Connection getconnection () {
Connection conn = null; Connection is a class,
Connections is a vector that stores the connection objects that are available for all idle states
if (connections.size () > 0) {

Get the first connection to a list of connections
conn = (Connection) connections.elementat (0);
Connections.removeelementat (0);//Get a connection and remove the connection from the queue.

If this connection is turned off, just continue to fetch,
try {
if (conn.isclosed ())
conn = getconnection ();
}
catch (Exception e) {
E.printstacktrace ();
}
}
If the number of connections actually used is less than the maximum number of connections available, a new connection is added
else if (maxconn = 0 | | Using < MAXCONN) {
When there is no available connection (Maxconn = 0) and the number of connections is not up to the upper limit (using < Maxconn), a new connection is created
Conn=newconnection ();
}
Returns an empty pointer if the number of connections has reached the upper limit
if (conn!=null) {
using++;
}
Return conn;
}

/* Create a new connection * *
Public Connection newconnection () {
Connection Conn=null;
try{
Load Driver
Class.forName (drivername);
Conn=drivermanager.getconnection (DBID,USERNAME,PASSWD);
}catch (Exception e) {
E.printstacktrace ();
return null;
}
Return conn;
}

/* Close all connections *
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 {
List of connection pool names
Private vector poolnames =new vector ();
List of driver names
Private vector drivernames=new vector ();
List of database identities
Private vector dbids=new vector ();
List of user names
Private vector usernames=new vector ();
User password list
Private vector passwds=new vector ();
List of maximum connections
Private vector maxconns=new vector ();
Connection Pool Queue
Private Hashtable connpools=new Hashtable ();

Public Dbconnmanager () {
Add connection information for an Access database
Poolnames.addelement ("Access");
Drivernames.addelement ("Sun.jdbc.odbc.JdbcOdbcDriver");
Dbids.addelement ("jdbc:odbc:shopping");
Usernames.addelement ("");
Passwds.addelement ("");
Maxconns.addelement ("5");

Add connection information for SQL Server2000 database
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");

Connecting MySQL database information
Poolnames.addelement ("MySQL");
Drivernames.addelement ("Org.gjt.mm.mysql.Driver");
Dbids.addelement ("jdbc:mysql://localhost/shopping");
Usernames.addelement ("");
Passwds.addelement ("");
Maxconns.addelement ("5");

Connecting to the Oracle8i/9i database
Poolnames.addelement ("Oracle");
Drivernames.addelement ("Oracle.jdbc.driver.OracleDriver");
Dbids.addelement ("Jdbc:oracle:thin: @localhost: 1521:shopping");
Usernames.addelement ("");
Passwds.addelement ("");
Maxconns.addelement ("5");

Create a connection pool
Createpools ();
}

/* Returns the connection to the specified connection pool.
public void ReturnConnection (String name,connection conn) {
Dbconnpool pool= (Dbconnpool) connpools.get (name);
if (pool!=null)
{
Pool.returnconnection (conn);
}
}

/* Get a connection in a specified connection pool.
Public Connection getconnection (String name) {
Dbconnpool pool= (Dbconnpool) connpools.get (name);
if (pool!=null)
{
return Pool.getconnection ();
}
return null;
}

/* Close all connections *
Public synchronized void Closeconns () {
Enumeration allpools=connpools.elements ();
while (Allpools.hasmoreelements ()) {
Dbconnpool pool= (Dbconnpool) allpools.nextelement ();
Pool.closeconn ();
}
}

/* Create Connection Pool * *
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);
}
}
}

Here's the main page. JSP file

<%@ page contenttype= "text/html; CHARSET=GBK "%>
<%@ page import = "java.sql.*"%>
<jsp:usebean id= "Connmanager" scope= "Application" class= "Mybean". Dbconnmanager "/>
<title>
Shopping mall Home
</title>
<body bgcolor= "#B0C4DE" >
<center><%
Connection conna=connmanager.getconnection ("Access");
Connection conns=connmanager.getconnection ("sqlserver2000");
if (conns==null)
{
%>
The database is busy, please visit again later
<%
}
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>


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.