500分求解
问题描述:
通过JSP用连接池与数据库ORACLE8i连接,但是在连接的时候总是报错,驱动找不到,(工具是JBuilder7以及自带的Tomcat4)详细代码如下:(ConnectionPool.java,Login.jsp,Welcome.jsp)
//ConnectionPool.java
package testconnectionpool;
import java.sql.*;
import javax.servlet.http.*;
import oracle.jdbc.driver.OracleDriver;
public class ConnectionPool implements HttpSessionBindingListener
{
private Connection connection;
private Statement statement;
private String driver = "oracle.jdbc.driver.OracleDriver";
private String url = "jdbc:oracle:thin:@localhost:1521:accu";
private String user = "system";
private String password = "manager";
public ConnectionPool()
{
}
public void setDriver(String strDriver)
{
if(strDriver != null)
driver = strDriver;
}
public String getDriver()
{
return driver;
}
public void setUser(String strUser)
{
if(strUser != null)
user = strUser;
}
public String getUser()
{
return user;
}
public void setURL(String strURL)
{
if(strURL != null)
url = strURL;
}
public String getURL()
{
return url;
}
public void setPassword(String strPass)
{
if(strPass != null)
password = strPass;
}
public void getConn()
{
try
{
Class.forName(driver);
connection = DriverManager.getConnection(url,user,password);
statement = connection.createStatement();
}
catch(ClassNotFoundException eClass)
{
System.out.println("Driver--" + driver + "not found!");
connection = null;
}
catch(SQLException eSQL)
{
System.out.println("Connection cant get");
connection = null;
}
}
public Connection getConnection()
{
if(connection == null)
getConn();
return connection;
}
public ResultSet executeQuery(String sql) throws SQLException
{
if(connection == null || connection.isClosed())
getConn();
return statement.executeQuery(sql);
}
public int executeUpdate(String sql) throws SQLException
{
if(connection == null || connection.isClosed())
getConn();
return statement.executeUpdate(sql);
}
public void commit()
{
try
{
connection.commit();
}
catch(SQLException e)
{
e.printStackTrace();
}
}
public void rollback()
{
try
{
connection.rollback();
}
catch(SQLException ex)
{
ex.printStackTrace();
}
}
public void setAutoCommit(boolean flag)
{
try
{
connection.setAutoCommit(flag);
}
catch(SQLException e)
{
e.printStackTrace();
}
}
public void close()
{
try
{
if(connection != null)
connection.close();
}
catch(SQLException e)
{
e.printStackTrace();
}
}
public void valueBound(HttpSessionBindingEvent event)
{
try
{
if(connection == null || connection.isClosed())
{
connection = DriverManager.getConnection(url,user,password);
statement = connection.createStatement();
}
}
catch(SQLException e)
{
e.printStackTrace();
}
}
public void valueUnbound(HttpSessionBindingEvent event)
{
try
{
if(connection != null || !connection.isClosed())
connection.close();
}
catch(SQLException e)
{
e.printStackTrace();
}
finally
{
connection = null;
}
}
}
//Login.jsp
<%@ page contentType="text/html; charset=GBK" %>
<html>
<head>
<title>
Login
</title>
</head>
<body>
<h1>
Connection Pool Test!
</h1>
<form action="Welcome.jsp" method="post">
<br><br>
UserName:
<input type="input" name="name">
<br><br>
Password:
<input type="input" name="password">
<br><br>
<input type="submit" name="Submit" value="Submit">
<input type="reset" value="Reset">
</form>
</body>
</html>
//Welcome.jsp
<%@ page contentType="text/html; charset=GBK"
import="testconnectionpool.ConnectionPool"%>
<html>
<head>
<title>
Welcome
</title>
</head>
<body>
<h1>
Welcome you!
</h1>
<%String name="";
String password ="";
String status="";
String str="select id,name from system.xiaozhu where id =" + password +"and name="+name;
name = request.getParameter("name");
password = request.getParameter("password");
ConnectionPool CP = new ConnectionPool();
java.sql.ResultSet rs = CP.executeQuery(str);
if(rs == null)
status = "error";
else
status = "Welcome you," + name;
%>
<br>
<br>
</body>
</html>