马栩周培训班:JSP中 bean的问题

来源:百度文库 编辑:中科新闻网 时间:2020/01/20 14:58:01
为什么当我include这个bean后
在JSP页面引用Query方法时是对的 可以查询数据
引用Uqdata就会出错啊
bean如下:
package bean;
import java.sql.*;
public class DataBase {
String driver="org.gjt.mm.mysql.Driver";
String sConnStr="jdbc:mysql://localhost/osic";
Connection connect=null;
ResultSet rs=null;
int value=0;
public DataBase()
{
try
{
Class.forName(driver);
}
catch(java.lang.ClassNotFoundException e)
{
e.printStackTrace();
}
}

public ResultSet Query(String sql)
{
try{

connect=DriverManager.getConnection(sConnStr,"root","1");
Statement stmt=
connect.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs=stmt.executeQuery(sql);
}
catch(SQLException e){

e.printStackTrace();
}
return rs;
}

public int Uqdata(String sql){

try{

connect=DriverManager.getConnection(sConnStr,"root","1");
Statement stmt=
connect.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
value=stmt.executeUpdate(sql);
}
catch(SQLException e){

e.printStackTrace();
}
return value;
}

}
下面是引用Query方法页面代码:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<html>
<jsp:useBean id="db" scope="page" class="bean.DataBase" />
<head>
<title>游客留言</title>
</head>
<body>
<%
String sql="select * from users";
ResultSet rs=db.Query(sql);
while(rs.next()){
out.println(rs.getString("user_name"));
}
%>
</body>
</html>
这个是对的可以得出查询结果

引用Uqdata就会出错,JSP页面代码如下:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<html>
<jsp:useBean id="db" scope="page" class="bean.DataBase" />
<head>
<title>游客留言</title>
</head>
<body>
<%
int value=0;
String sql="delete from users where ID='2'";
value=db.Uqdata(sql);
if(value>0)
{
out.println("修改成功!<a href=userlist.jsp>请返回</a>");
}else
{
out.println("修改失败!<a href=userlist.jsp>请返回</a>");
}
%>
</body>
</html>

这样就会出错
exception

org.apache.jasper.JasperException
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:372)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

root cause

java.lang.NullPointerException
org.apache.jsp.ad.menu_005fedit1_jsp._jspService(menu_005fedit1_jsp.java:82)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:324)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
现在出的错是直接 执行了 out.println("修改失败!<a href=userlist.jsp>请返回</a>");

空指针异常,检查数据库里面是否有你要删除的值!测试语句是否调用传入了JAVABEAN执行,如果实在找不到错 到TOMCAT目录下 找到此工程的文件夹 查看此JSP编译后的servlet代码
menu_005fedit1_jsp.java:82 就能知道了!

出错信息是什么,还是它执行 out.println("修改失败!<a href=userlist.jsp>请返回</a>");

把出错的信息贴出来啊,不然不好帮你。

有空指针!你自己找找看,有什么漏洞把