调用JavaBean的问题(100分)

  • 主题发起人 主题发起人 samzw
  • 开始时间 开始时间
S

samzw

Unregistered / Unconfirmed
GUEST, unregistred user!
程序出错:java.lang.NullPointerException
程序如下:
package test;
import java.sql.*;
public class DBCon
{
String sDBDriver="com.microsoft.jdbc.sqlserver.SQLServerDriver";
//需要你加载sql server的包
String sConnStr="jdbc:microsoft:sqlserver://127.0.0.1:1433;databaseName=Northwind";

private Connection conn=null;
private ResultSet rs=null;
private Statement stmt=null;
public void DBCon()
{
try
{
Class.forName(sDBDriver);
}
catch(java.lang.ClassNotFoundException e)
{
System.out.println("DBcon():"+e.getMessage());
}
}
public ResultSet executeQuery(String sql)
{
try
{
conn=DriverManager.getConnection(sConnStr,"sa","samzw");
stmt=conn.createStatement();
rs=stmt.executeQuery(sql);
}
catch(SQLException ex)
{
System.out.println("aq.executeQuery:"+ex.getMessage());
}
return rs;
}

public void closeStmt()
{
try
{
stmt.close();
}
catch(SQLException e)
{
e.printStackTrace();
}
}
public void closeConn()
{
try
{
conn.close();
}
catch(SQLException e)
{
e.printStackTrace();
}
}
}
<%@page language="java" import="java.sql.*"%>
<jsp:useBean id="DBConn1" scope="page" class="test.DBCon"/>
<html>
<head>
<title>JSP Test </title>
</head>
<body>
<%
ResultSet RS=DBConn1.executeQuery("select * from guestbook");
if (RS==null) out.print("NullPointerException");
while (RS.next())
{
out.print(""+RS.getString("customerid")+"");
out.print(""+RS.getString("companyname")+"");
out.print(""+RS.getString("ContactName")+"");
}
RS.close();
%>
</body>
</html>
程序编译没问题,class路径也没问题(不然,一大堆找不到class的错误),可在IE下提示
java.lang.NullPointerException,请高手指点
 
你要把所有的错误信息都帖出来啊,光说一个异常,有什么用啊?
提示的错误不止一行吧?都帖出来。
 
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 org.apache.jsp.index$jsp._jspService(index$jsp.java:90)
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:201)
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.valves.CertificatesValve.invoke(CertificatesValve.java:246)
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:536)
 
public void DBCon()//这一句看起来有问题的,
应该是public DBCon()
作为构造函数,它应该是与类名相同的,而且不带返回值的修饰符,因为它将确定
返回类的一个实例。
 
to 小猪
还是不对,public DBCon()已经改过来了
 

catch(java.lang.ClassNotFoundException e)//这里改为截获Exception ,看看有没有其他什么异常
{
System.out.println("DBcon():"+e.getMessage());
}
 
莫名其妙就好了,实在搞不懂
 
接受答案了.
 
后退
顶部