500分求解

littlef 2003-08-22 02:33:11
问题描述:
通过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>

...全文
29 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
yuxiangyu 2003-08-23
  • 打赏
  • 举报
回复
大哥,首先,我想知道你报的错具体是什么?
然后我觉得你为什么不用<useBean>的写法呢?这样的话,可以用scope=session将连接保存在session里面。因为你实现了valueBound这个方法,每次请求页面的时候Connection对象一定是空的。所以一定会建立一个连接池。所以我建议你先单独测一下这个javaBean写一个main方法测一下看看报的错是什么?
orant 2003-08-23
  • 打赏
  • 举报
回复
我帮你试试看
oldheroxu 2003-08-22
  • 打赏
  • 举报
回复
在oracle的安装目录下找到Ora81\jdbc\lib\classes12.zip文件(内有你想要的驱动),
将这个文件拷贝到tomcat中你所部署的web-inf目录下的lib目录(如果没有就建一个)下即可。
祝你成功!
javaboy 2003-08-22
  • 打赏
  • 举报
回复
是否将ORACLE驱动程序放在classpath了

81,091

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧