帮忙找错...很简单的 (50分)

  • 主题发起人 主题发起人 cdzerg
  • 开始时间 开始时间
C

cdzerg

Unregistered / Unconfirmed
GUEST, unregistred user!
1 按照[red]附录一[/red]的文章写的jsp和javabean
2 在浏览器上有[red]附录二[/red]的错误
3 Tomcat 4.0.4 sqlServer2000 IE5.0

[red]附录一[/red]
在JSP中访问数据库方法
作者:林华 本文选自:赛迪网 2002年02月20日

JSP(JavaServer Pages)是由Sun 公司倡导、许多公司参与建立的一种动态网页技术标准。使用JSP技术,Web页面开发人员可以使用HTML或者XML标识来设计和格式化最终页面。使用JSP标识(tag)或者小脚本(Scriptlet)来生成页面上的动态内容。生成内容的逻辑被封装在标识和JavaBeans组件中,并且捆绑在小脚本中,所有的脚本在服务器端运行。
数据库连接对动态网站来说是最为重要的部分,Java中连接数据库的技术是JDBC(Java Database Connectivity)。很多数据库系统带有JDBC驱动程序,Java程序就通过JDBC驱动程序与数据库相连,执行查询、提取数据等操作。Sun公司还开发了JDBC-ODBC bridge,用此技术Java程序就可以访问带有ODBC驱动程序的数据库,目前大多数数据库系统都带有ODBC驱动程序,所以Java程序能访问诸如Oracle、Sybase、MS SQL Server和MS Access等数据库。本文将通过一个实例介绍在JSP中利用JavaBeans通过JDBC-ODBC Bridge访问Access客户信息数据库。
1.首先建立一个Access数据库Customers.mdb,其中表Customers有字段id(自动增量型,并设为主关键字)、name(文本型,长度10)、address(文本型,长度30)、info(备注型)。
2.在Control Panel(控制面板)的ODBC Datasource模块中加入System DSN,取名Customers,并指向Customers.mdb。
3.创建一个JavaBeans,命名为DBconn.java,并保存在支持JSP的Web服务器的缺省文档根目录下。DBconn.java主要是封装与数据库的连接操作,内容如下:
import java.sql.*;
public class DBconn {
String DBDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
String ConnStr = "jdbc:odbc:Customers";
Connection conn = null;
ResultSet rs = null;
public DBconn {
try {
Class.forName(DBDriver);
//加载数据库驱动程序
}
catch(java.lang.ClassNotFoundException e) {
System.err.println("DBconn (): " + e.getMessage());
}
}
public ResultSet executeQuery(String sql) {
rs = null;
try {
conn = DriverManager.getConnection(ConnStr);
//与DBMS建立连接
Statement stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
}
catch(SQLException ex) {
System.err.println("aq.executeQuery: " + ex.getMessage());
}
return rs;
}
}

4.DBconn.java编辑好后,在DOS状态下,进而用JDK的javac命令编译DBconn.java形成相应的class文件。
5.建立Customers.jsp文件,在JSP中调用以上编译好的JavaBeans,其内容如下:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=gb2312">
<title>客户信息调查</title>
</head>
<body>
<p><b>客户信息调查</b></p>
<%@ page language="java" import="java.sql.*" %>
<jsp:useBean id="DBconn1" scope="page" class="DBconn" />
<%
ResultSet RS = DBconn1.executeQuery("SELECT * FROM Customers");
while (RS.next()) {
out.print("<LI>" + RS.getString("name") + "</LI>");
out.print("<LI>" + RS.getString("address") + "</LI>");
out.print("<LI>" + RS.getString("info") + "</LI>");
}
RS.close();
%>
</body>
</html>

在<jsp:useBean>标记内定义了几个属性,其中id是整个JSP页面内该Bean的标识、scope属性定义了该Bean的生存时间、class属性说明了该Bean的类文件。

[red]附录二[/red]
Apache Tomcat/4.0.4 - HTTP Status 500 - Internal Server Error
--------------------------------------------------------------------------------
type Exception report
message Internal Server Error
description The server encountered an internal error (Internal Server Error) that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: Unable to compile class for JSP
An error occurred at line: 9 in the jsp file: /jsp/customer/customer.jsp
Generated servlet error:
D:/tools/JBuilder7/jakarta-tomcat-4.0.4/work/Standalone/localhost/examples/jsp/customer/customer$jsp.java:65: Class org.apache.jsp.DBconn not found.
DBconn DBconn1 = null;
^

An error occurred at line: 9 in the jsp file: /jsp/customer/customer.jsp
Generated servlet error:
D:/tools/JBuilder7/jakarta-tomcat-4.0.4/work/Standalone/localhost/examples/jsp/customer/customer$jsp.java:68: Class org.apache.jsp.DBconn not found.
DBconn1= (DBconn)
^

An error occurred at line: 9 in the jsp file: /jsp/customer/customer.jsp
Generated servlet error:
D:/tools/JBuilder7/jakarta-tomcat-4.0.4/work/Standalone/localhost/examples/jsp/customer/customer$jsp.java:73: Class org.apache.jsp.DBconn not found.
DBconn1 = (DBconn) java.beans.Beans.instantiate(this.getClass().getClassLoader(), "DBconn");
^
3 errors
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:285)
at org.apache.jasper.servlet.JspServlet.loadJSP(JspServlet.java:548)
at org.apache.jasper.servlet.JspServlet$JspServletWrapper.loadIfNecessary(JspServlet.java:176)
at org.apache.jasper.servlet.JspServlet$JspServletWrapper.service(JspServlet.java:188)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:381)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:473)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:247)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:243)
at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:190)
at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)
at org.apache.catalina.authen
ticator.Authen
ticatorBase.invoke(Authen
ticatorBase.java:475)
at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2347)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:180)
at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)
at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:170)
at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:170)
at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:468)
at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:174)
at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
at org.apache.catalina.connector.http.HttpProcessor.process(HttpProcessor.java:1027)
at org.apache.catalina.connector.http.HttpProcessor.run(HttpProcessor.java:1125)
at java.lang.Thread.run(Thread.java:484)

--------------------------------------------------------------------------------
 
你把DbConn.class放在什么地方了?
是不是web-inf/classes/DbConn.class?
 
jsp文件在 /webapps/customer/customer.jsp
class放在 /webapps/customer/WEB_INF/classes/DBConn.class
 
给你的DbConn加上一个包吧。
比如
package my.test;
然后放在/webapps/customer/WEB_INF/classes/my/test/DBConn.class
在你的jsp里面加上
<%@ page import="my.test.*"%>
 
多人接受答案了。
 
后退
顶部