Jsp连接Access数据库问题,帮帮忙,急

JlinShu 深圳市卓亮科技有限公司 研发部门经理  2006-08-04 11:35:33
//我想测试一下数据库边接,半天没有结果,头都大了,高手帮帮忙,急。
//下面是Default.jsp的内容,和数据库连接代码

<%@ page contentType="text/html;charset=GBK" %>
<%@ page language="java" import="java.sql.*" %>
<jsp:useBean id="connBean" scope="page" class="db.ConnAccess"/>
<html>
<head>
<title>bean测试</title>
</head>
<body>
<table width="327" border="1">
<tr align="center">
<td width="89"><div align="center">Id</div></td>
<td width="108"><div align="center">用户名</div></td>
<td width="97"><div align="center">密码</div></td>
</tr>
<%
ResultSet RS_result=connBean.executeQuery("select * from UserInfo");
String Name1="";
String Pwd1="";
while(RS_result.next())
{
Name1=RS_result.getString("userName");
Pwd1=RS_result.getString("userPassword");
%>
<tr align="center">
<td><div align="center"><%=Name1%></div></td>
<td><div align="center"><%=Pwd1%></div></td>
</tr>
<%
}
RS_result.close();
%>
</table>
</body>
</html>


//以下是我的数据库连接bean,生成的ConnAccess.class我放到了WEB-INF/classes/db目录下,可是打
//测试的时候总是提示
//The server encountered an internal error () that prevented it from fulfilling this request.
//请问是什么原因,我该怎么做?

package db;
import java.sql.*;
public class ConnAccess{
String DbDriver="sun.jdbc.odbc.JdbcOdbcDriver";
String ConnStr="jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=test.mdb";
Connection connect=null;
ResultSet rs=null;
public ConnAccess()
{
try {
Class.forName(DbDriver);
}
catch(java.lang.ClassNotFoundException e)
{
System.err.println(e.getMessage());
}
}

public ResultSet executeQuery(String sql)
{
try {
connect=DriverManager.getConnection(ConnStr);
Statement stmt=connect.createStatement();
rs=stmt.executeQuery(sql);
}
catch(SQLException ex)
{
System.err.println(ex.getMessage());
}
return rs;
}

public int executeUpdate(String sql)
{
int result=0;
try {
connect=DriverManager.getConnection(ConnStr);
Statement stmt=connect.createStatement();
result=stmt.executeUpdate(sql);
}
catch(SQLException ex)
{
System.err.println(ex.getMessage());
}
return result;
}
}
...全文
655 点赞 收藏 32
写回复
32 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
alison007 2006-08-09
maka007(零下一度)
可以用相对路径,给段代码大家学习:
Connection Conn = null;
try{
StringBuffer accessURL = new StringBuffer("jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=");
accessURL.append("test.mdb");
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Conn = DriverManager.getConnection(accessURL.toString());
}
catch (Exception e){
System.err.println("mydbAccess(): " + e.getMessage());
}
回复
maka007 2006-08-05
mdb数据库不允许用相对路径?
那发布的时候怎么办?我当时就碰到这个问题,直接换成SQL数据库了
回复
JlinShu 2006-08-05
数据库里有记录的
回复
JlinShu 2006-08-05
提示以下错误,应该是我的Bean没放好,我放在web-inf/classess下面,怎么会这样?


C:\Program Files\Apache Group\Tomcat 4.1\work\Standalone\localhost\KangDa\default_jsp.java:44: 找不到符号
符号: 类 ConnAccess
位置: 类 org.apache.jsp.default_jsp
ConnAccess connBean = null;
^



An error occurred at line: 2 in the jsp file: /default.jsp

Generated servlet error:
C:\Program Files\Apache Group\Tomcat 4.1\work\Standalone\localhost\KangDa\default_jsp.java:46: 找不到符号
符号: 类 ConnAccess
位置: 类 org.apache.jsp.default_jsp
connBean = (ConnAccess) pageContext.getAttribute("connBean", PageContext.PAGE_SCOPE);
^



An error occurred at line: 2 in the jsp file: /default.jsp

Generated servlet error:
C:\Program Files\Apache Group\Tomcat 4.1\work\Standalone\localhost\KangDa\default_jsp.java:49: 找不到符号
符号: 类 ConnAccess
位置: 类 org.apache.jsp.default_jsp
connBean = (ConnAccess) java.beans.Beans.instantiate(this.getClass().getClassLoader(), "ConnAccess");
^
3 错误
回复
kevinliuu 2006-08-04
没取到连接

String ConnStr="jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=test.mdb";

你的mdb文件要写绝对路径
回复
JlinShu 2006-08-04
???不太懂,能具体点吗?
回复
kevinliuu 2006-08-04
空指针了

回复
JlinShu 2006-08-04
以上是全部错误信息
回复
JlinShu 2006-08-04
type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:207)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:240)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:187)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:809)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:200)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:146)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:209)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:596)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:433)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:948)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:144)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:596)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:433)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:948)
at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2358)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:133)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:596)
at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:118)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:594)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:116)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:594)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:433)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:948)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:127)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:596)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:433)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:948)
at org.apache.coyote.tomcat4.CoyoteAdapter.service(CoyoteAdapter.java:152)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:799)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:705)
at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:577)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:683)
at java.lang.Thread.run(Unknown Source)


root cause

java.lang.NullPointerException
at org.apache.jsp.default_jsp._jspService(default_jsp.java:84)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:92)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:809)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:162)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:240)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:187)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:809)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:200)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:146)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:209)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:596)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:433)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:948)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:144)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:596)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:433)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:948)
at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2358)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:133)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:596)
at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:118)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:594)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:116)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:594)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:433)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:948)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:127)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:596)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:433)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:948)
at org.apache.coyote.tomcat4.CoyoteAdapter.service(CoyoteAdapter.java:152)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:799)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:705)
at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:577)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:683)
at java.lang.Thread.run(Unknown Source)

回复
interpb 2006-08-04
把所有的页面错误信息贴出来
回复
lndltw 2006-08-04
是不是你数据库里现在还没有记录啊?
回复
will123 2006-08-04
还是要慢慢学基础才行啊,我
回复
windcash111 2006-08-04
没错的 我测试过的 如果你要封装javabean应该没什么难度把
我就发现你的连接方式可能有点问题,还有你的数据库的路径
回复
windcash111 2006-08-04
<%@ page contentType="text/html; charset=GBK" %>
<%@ page import="java.sql.*" %>
<html>
<body>
<%
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url="jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ="+
"F:\\dbconn.mdb";//这里只要指定到你数据库dbconn.mdb的位置就可以
String user="";
String password="";
Connection conn=DriverManager.getConnection(url,user,password);
Statement stmt=conn.createStatement();
String sql="select * from UserInfo";
ResultSet rs=stmt.executeQuery(sql);%>
<center><h2>用JDBC连接SQLServer的方法</h2></center>
<hr>
<%
while(rs.next()){
%>
您的第一个字段内容为:<%=rs.getString(1)%>
您的第二个字段内容为:<%=rs.getString(2)%><br>
<%}%>
<% out.print("数据库操作成功,恭喜你");%>
<% rs.close();
stmt.close();
conn.close();
%>
</body>
</html>
回复
ad8326 2006-08-04
你数据库文件路径有问题,DBQ=D:/test.mdb 而且要注意 文件名不能有空格
回复
alison007 2006-08-04
还有这段:Statement stmt=connect.createStatement();
改成:Statement stmt = connect.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
这样会减少很多问题
回复
alison007 2006-08-04
路径不能直接 DBQ=test.mdb
要具体的地址比如:DBQ=D:/test.mdb
注意:是用“/”,不能用“\”
回复
xzjjy 2006-08-04
应该是数据库文件的路径问题
回复
JlinShu 2006-08-04
Exception in thread "main" java.lang.NoClassDefFoundError:ConnAccess(wrong name:db/ConnAcess)
这又是怎么回事?
回复
JlinShu 2006-08-04
哦,不能加Class,哎,真晕
回复
加载更多回复
相关推荐
发帖
Web 开发
创建于2007-09-28

8.0w+

社区成员

Java Web 开发
申请成为版主
帖子事件
创建了帖子
2006-08-04 11:35
社区公告
暂无公告