我用JAVABEANS连接数据库,运行时报错,什么原因啊?

aihuafly 2004-09-04 12:04:04
message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:358)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:301)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:248)
javax.servlet.http.HttpServlet.service(HttpServlet.java:856)


root cause

java.lang.NullPointerException
org.apache.jsp.index_jsp._jspService(index_jsp.java:70)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:133)
javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:311)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:301)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:248)
javax.servlet.http.HttpServlet.service(HttpServlet.java:856)


note The full stack trace of the root cause is available in the Tomcat logs.

...全文
134 点赞 收藏 12
写回复
12 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
alaal 2004-09-04
是你的jsp里的空指针错误

看看这些是否为null

request.getParameter("username")
request.getParameter("password")

然后看这几个
login.executeQuery(sql);
rs.close();
login.closeConn();
login.closeStmt();

在jsp里多设一个段点打印点东西,看看是哪里间断了
这种调试问题都得靠你自己跟踪调试,别人帮不上忙的
回复
fanqingfeng 2004-09-04
if(rs.next())
{
rs.close();
login.closeConn();/////////////
login.closeStmt();///////////////
out.println("用户名密码错误!");
}
%>


上面两个的关闭调换过来才行 不然 关闭了conn 哪来的stmt呢
回复
Tongates 2004-09-04
以下的原因只是猜测:

1)实例名和类名一样
<jsp:useBean id="login" scope="page" class="erp.login" />
改为
<jsp:useBean id="loginInstance" scope="page" class="erp.login" />
试一试

2)在jsp页面中,将下列的改一下看看

ResultSet rs=login.executeQuery(sql);

if(rs.next())
这句话改为:
if( null != rs && rs.next() )


3)下面的
{
rs.close();
login.closeConn();
login.closeStmt();
out.println("用户名密码错误!");
}
改为:

{
rs.close();
loginInstance.closeStmt();
loginInstance.closeConn();
out.println("用户名密码错误!");
}


4)在login这个类里面
close之前判断statement和connection是否是null
回复
aihuafly 2004-09-04
哪有空对象啊?
javabeans:login.java
package erp;
import java.sql.*;
import java.lang.*;
import java.util.*;

public class login {
String sDBDriver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
String sConnStr = "jdbc:sqlserver://localhost:1433;DatabaseName=mophy;user=sa;password=123456";
private Connection conn = null;
private Statement stmt =null;
ResultSet rs = null;
//注册数据库驱动程序
public login(){
try{
Class.forName(sDBDriver);
}
catch(ClassNotFoundException e)
{
System.out.println("无法建立数据库连接!"
+e.getMessage());
}
}
//建立数据库连接及定义数据查询
public ResultSet executeQuery(String sql)
{
rs = null;
try{
//sql = new String(sql.getBytes("GBK"),"ISO8859_1");
conn = DriverManager.getConnection(sConnStr);
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
}
catch(SQLException ex)
{
System.out.println("执行查询出错!"+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();
}
}
}


jsp:
<%@ page contentType="text/html; charset=GBK" %>
<%@ page language = "java" %>
<%@ page import ="java.sql.*" %>
<html>
<head>
<title>
index
</title>
</head>
<jsp:useBean id="login" scope="page" class="erp.login" />
<jsp:setProperty name="login" property="*" />
<%
String uname = new String(request.getParameter("username").getBytes("8859_1"));
String pwd = new String(request.getParameter("password").getBytes("8859_1"));
String sql = "select name from erp_user where name='"+uname+"' and password='"+pwd+"'";
ResultSet rs=login.executeQuery(sql);
if(rs.next())
{
rs.close();
login.closeConn();
login.closeStmt();
out.println("用户名密码错误!");
}
%>
<body bgcolor="#ffffff">
<h1><center>
欢迎光临
</center>
</h1>
<form method="post">
<table>
<tr>
<td>用户名:</td><td><input type="text" name="username" size="16"></td>
</tr>
<tr>
<td>密 码:</td><td><input type="password" name="password" size = "16"></td>
</tr>
<tr>
<td><input type="submit" name="submit" value="登录"></td>
<td><input type="reset" name="reset" value="取消"></td>
</tr>
</table>
</form>
</body>
</html>

弄了一天了,还是不出结果
回复
黑马 2004-09-04
有对象为null

NullPointerException
回复
cocohui 2004-09-04
我用了楼主的代码报如下的错误!
org.apache.jasper.JasperException: /admin/text2.jsp(10,0) The value for the useBean class attribute newworld.login is invalid.
org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:39)
org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:407)
org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:150)

是不是环境变量的问题啊!!大家看看!
回复
boby1863 2004-09-04
同意lguo_714(凌) ,最好加上判断看rs是否为空啊
回复
lguo_714 2004-09-04
if(rs.next())
{
rs.close();
login.closeConn();/////////////
login.closeStmt();///////////////
out.println("用户名密码错误!");
}
%>


上面两个的关闭调换过来才行 不然 关闭了conn 哪来的stmt呢
回复
seacaptain 2004-09-04
gz
回复
aihuafly 2004-09-04
ResultSet rs = loginbean.executeQuery(sql)
执行后rs为null
应该rs是什么值?
是不是表示没有连上数据库?
回复
aihuafly 2004-09-04
谢谢各位了.各位的方法都试了.
应该是rs.next()处有问题.
具体怎么解决还是不知道.
回复
lguo_714 2004-09-04
TO: aihuafly(水中魚) "弄了一天了,还是不出结果", 你到底想要什么结果啊?

按照Tongates(文文)的修改一下,javabeans的名字不用改,另外

String uname = new String(request.getParameter("username").getBytes("8859_1"));
String pwd = new String(request.getParameter("password").getBytes("8859_1"));
String sql = "select name from erp_user where name='"+uname+"' and password='"+pwd+"'";

将以上代码改为:

String uname=request.getParameter("username");
String pwd=request.getParameter("password");
if (uname!=null)
uname = new String(uname).getBytes("8859_1"));
if (pwd!=null)
pwd = new String(pwd ).getBytes("8859_1"));
回复
相关推荐
发帖
Web 开发
创建于2007-09-28

8.0w+

社区成员

Java Web 开发
申请成为版主
帖子事件
创建了帖子
2004-09-04 12:04
社区公告
暂无公告