我是一个新手,初写JSP+JAVABEAN的网页,出现了一个很头疼的问题,请高手帮帮忙!
先帖源代码:
1,操作数据库的JAVABEAN
package library;
import java.sql.*;
public class connDB {
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
public connDB(){
try {
Class.forName( "com.jnetdirect.jsql.JSQLDriver ");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
//执行查询操作,返回值为ResultSet
public ResultSet executeQuery(String sql)
{
try{
conn=DriverManager.getConnection( "jdbc:JSQLConnect://127.0.0.1:1433/database=Library ", "sa ", "sa ");
stmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs=stmt.executeQuery(sql);
}catch(SQLException e)
{
System.err.println(e.getMessage());
}
return rs;
}
//执行修改操作,返回值为int
public int executeUpdate(String sql){
int result=0;
try{
conn=DriverManager.getConnection( "jdbc:JSQLConnect://127.0.0.1:1433/database=Library ", "sa ", "sa ");
stmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
result=stmt.executeUpdate(sql);
}catch(SQLException e)
{
System.err.println(e.getMessage());
}
return result;
}
//执行关闭操作
public void close(){
try{
if(rs!=null)rs.close();
}catch(Exception e)
{
e.printStackTrace(System.err);
}
try{
if(stmt!=null)stmt.close();
}catch(Exception e)
{
e.printStackTrace(System.err);
}
try{
if(conn!=null)conn.close();
}catch(Exception e){
}
}
}
2,从表单获取用户ID(managerid),与密码(managerpassword),下面是进行用户验证的JSP。
<%@ page contentType= "text/html; charset=GBK " %>
<%@ page import= "library.* " %>
<%@ page import= "java.sql.* " %>
<jsp:useBean id= "connDB " scope= "page " class= "library.connDB " />
<jsp:setProperty name= "connDB " property= "* " />
<jsp:useBean id= "chStr " scope= "page " class= "library.chStr "/>
<jsp:setProperty name= "chStr " property= "* "/>
<jsp:useBean id= "LibraryManager " scope= "page " class= "library.LibraryManager "/>
<jsp:setProperty name= "LibraryManager " property= "* "/>
<%
String managerid=request.getParameter( "managerid ");
String managerpassword=request.getParameter( "managerpassword ");
String sql= "select * from T_Manager where ManagerUID= "+managerid+ "and Password= "+managerpassword+ " ";
ResultSet rs_manager=connDB.executeQuery(sql);
if(!rs_manager.next()){
rs_manager.close();%>
<jsp:forward page= "B_Fail.jsp "> </jsp:forward>
<%}else{
String id=rs_manager.getString(1);
String name=rs_manager.getString(2);
String key=rs_manager.getString(3);
String type=rs_manager.getString(4);
session.setAttribute( "id ", id);
session.setAttribute( "name ", name);
session.setAttribute( "key ",key);
session.setAttribute( "type ",type);
if(type.equals( "超级管理员 "))
{response.sendRedirect( "B_SuperManager.jsp ");rs_manager.close();}
else if(type.equals( "管理员一 "))
{response.sendRedirect( "B_ManagerTypeOne.jsp ");rs_manager.close();}
else {response.sendRedirect( "B_Login.jsp ");rs_manager.close();}
}
%>
3 ,程序报告如下:
HTTP Status 500 -
--------------------------------------------
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: Exception in JSP: /B_Switch.jsp:16
13: String managerpassword=request.getParameter( "managerpassword ");
14: String sql= "select * from T_Manager where ManagerUID= "+managerid+ "and Password= "+managerpassword+ " ";
15: ResultSet rs_manager=connDB.executeQuery(sql);
16: if(!rs_manager.next()){
17: rs_manager.close();%>
18: <jsp:forward page= "B_Fail.jsp "> </jsp:forward>
19: <%}else{
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:506)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:395)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
root cause
java.lang.NullPointerException
org.apache.jsp.B_005fSwitch_jsp._jspService(B_005fSwitch_jsp.java:91)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:334)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
note The full stack trace of the root cause is available in the Apache Tomcat/5.5.20 logs.
--------------------------------------------
Apache Tomcat/5.5.20
4,我不知道这是怎么一回事,是空指针问题??应该怎么改呀,我是头大了,搞了整整一天了!!!
[解决办法]
既然是NullPointerException,既是说connDB.executeQuery(sql);返回的是null
原因可能是JDBC驱动没有加载成功之类
即Class.forName( "com.jnetdirect.jsql.JSQLDriver ");可能出错,可能没有把相关的jar放到classpath里
当然也可能是其它原因引起的。
你看看log或者控制台输出才能搞清楚啊!
[解决办法]
String sql= "select * from T_Manager where ManagerUID= "+managerid+ "and Password= "+managerpassword+ " ";
and前面少空格,sql语句应该语法不对,把这个改了看看先 " and ...
[解决办法]
String sql= "select * from T_Manager where ManagerUID= ' "+managerid+ " ' and Password= ' "+managerpassword+ " ' ";
两边少单引号.ManagerUID和 Password 是什么类型的啊?
我的异常网推荐解决方案:The server encountered an internal error () that prevented it from fulfilling this request.,http://www.myexception.cn/java-web/317.html