Tomcat4.0环境下,在JSP中运用Bean出现错误,请各位大侠指教?谢谢!

Leony 2002-06-21 03:59:25
我写了一个应用Bean的JSP程序,包括DbConnection.java、ViewQueryBean.java和
ViewQuery.jsp,其中头两个文件存放在目录webapps/examples/web-inf/classes/aboutjspdb下(已经编译过了),后面一个存放在目录webapps/examples/jsp/database
DbConnection.java如下所示:
package aboutjspdb;

import java.util.*;
import java.sql.*;
import java.io.*;

public class DbConnection
{
Connection conn = null;
Statement stmt = null;
ResultSet rset = null;

public DbConnection()
{
}

public boolean openConnection()
{
String url=new String("jdbc:odbc:Instore Army");
String user="sa";
String password="";
try
{
Class.forName("sun.jdbc:odbc:JdbcOdbcDriver");
}catch(ClassNotFoundException e){
System.out.println("JDBC登录过程中出现错误:"+e.getMessage());
return false;
}

try{
this.conn=DriverManager.getConnection(url,user,password);
}catch(SQLException e){
System.out.println("生成Connection过程中出现错误:"+e.getMessage());
return false;
}

return true;
}

public ResultSet executeQuery(String query) throws SQLException
{
this.stmt = conn.createStatement();
this.rset = stmt.executeQuery(query);
return rset;
}

public void executeUpdate(String query) throws SQLException
{
this.stmt = conn.createStatement();
stmt.executeUpdate(query);
if(stmt!=null) stmt.close();
}

public void close() throws SQLException
{
if(conn!=null) conn.close();
if(rset!=null) rset.close();
if(stmt!=null) stmt.close();
}
protected void finalize() throws Throwable
{
this.close();
}
}
ViewqueryBean如下所示:
package aboutjspdb;

import java.sql.*;

public class ViewQueryBean
{
aboutjspdb.DbConnection dc = null;
ResultSet rset = null;
public ViewQueryBean()
{
dc = new aboutjspdb.DbConnection();
}
public boolean openConnection()
{
return dc.openConnection();
}
public void executeQuery(String query) throws SQLException
{
this.rset = dc.executeQuery(query);
}

public void executeUpdate(String query) throws SQLException
{
dc.executeUpdate(query);
}

public int getColumnCount() throws SQLException
{
ResultSetMetaData rsmd = rset.getMetaData();
return rsmd.getColumnCount();
}

public String getColumnName(int index) throws SQLException
{
ResultSetMetaData rsmd=rset.getMetaData();
return rsmd.getColumnName(index);
}

public String getData(int index) throws SQLException
{
return rset.getString(index).trim();
}

public String getData(String columnName) throws SQLException
{
return rset.getString(columnName).trim();
}

public boolean next() throws SQLException
{
return rset.next();
}
public void close() throws SQLException
{
if(rset!=null) rset.close();
if(dc!=null) dc.close();
}
protected void finalize() throws Throwable
{
close();
}
}
ViewQuery.jsp如下所示:
<%@ page info= "[About JSP] DB Connection Bean 创作实例" contentType="text/html;charset=GB2312"
import="aboutjspdb.ViewQueryBean" %>

<jsp:useBean id="bean" scope="request" class="aboutjspdb.ViewQueryBean" />
<%
String query=
request.getParameter("query")==null?
"SELECT * from city":request.getParameter("query");
bean.openConnection();
bean.executeQuery(query);
%>

<HTML>
<body>

<!-- 检索Table -->
<H3> SQL 查询语句 </H3>
<form method=get action="ViewQuery.jsp">
<input size=80 name=query value="<%=query%>">
<input type=submit value="传送查询语句">
</form>

<HR>

<H3>结果 VIEW </H3>
<table border=1>
<!-- [开始] 显示Column名称-->
<tr>
<% for(int i=1;i<=bean.getColumnCount();i++) {%>
<td><%= bean.getColumnName(i) %></td>
<%}%>
</tr>
<!-- [结尾] 显示Column名称-->
<% while(bean.next()) {%>
<tr>
<% for(int i=1;i<=bean.getColumnCount();i++) {%>
<td><%=bean.getData(i)%></td>
<%}%>
</tr>
<%}%>
<!-- [结尾] 显示数据-->
</table>
</body>
</html>
当我在IE的url中打入:http://localhost:8080/examples/jsp/database/ViewQuery.jsp时出现如下错误:
Apache Tomcat/4.0.1 - 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

java.lang.NullPointerException
at aboutjspdb.DbConnection.executeQuery(DbConnection.java:106)
at aboutjspdb.ViewQueryBean.executeQuery(ViewQueryBean.java:31)
at org.apache.jsp.ViewQuery$jsp._jspService(ViewQuery$jsp.java:98)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:107)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.jasper.servlet.JspServlet$JspServletWrapper.service(JspServlet.java:202)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:382)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:474)
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:201)
at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
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:2344)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
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:462)
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:163)
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:1011)
at org.apache.catalina.connector.http.HttpProcessor.run(HttpProcessor.java:1106)
at java.lang.Thread.run(Unknown Source)

请问是什么意思,应该如何解决?谢谢!!!!!
...全文
35 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
Leony 2002-06-21
  • 打赏
  • 举报
回复
没有人能够帮到我吗????????
Leony 2002-06-21
  • 打赏
  • 举报
回复
那我应该怎么办呢?那错误是什么意思呀?多谢!
Leony 2002-06-21
  • 打赏
  • 举报
回复
可以的呀?
Andrawu 2002-06-21
  • 打赏
  • 举报
回复
你Bean和jsp都没有什么大的问题。
可能是Web服务的问题。
Andrawu 2002-06-21
  • 打赏
  • 举报
回复
try:

你先在IE的url中打入:http://localhost:8080/examples/jsp/database/
看能不能浏览文件。

<%@ page contentType="text/html;charset=GB2312"%>
<jsp:useBean id="bean" scope="request" class="aboutjspdb.ViewQueryBean" />
Leony 2002-06-21
  • 打赏
  • 举报
回复
Bean应该没有什么问题吧,是不是我JSP程序写错误了呢?刚刚运用Bean,请各位多多指导,不胜感激,谢谢!!!

81,092

社区成员

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

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