2 baidu 34118455 baidu_34118455 于 2016.03.28 20:36 提问

各位亲,帮帮忙看一下这个程序那里有问题,在浏览器中运行,rs.next()有错

这是dbBean页面
package com.regist;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class dbBean {
Connection con = null;
Statement stat = null;
ResultSet rs = null;
PreparedStatement pstat = null;

public dbBean(){}
public Connection getcon() throws SQLException{
    try {
        Class.forName("com.mysql.jdbc.Driver");
        String url = "jdbc:mysql://localhost:3306/land?user=root&password=root";
        con = DriverManager.getConnection(url);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return con;
}

//执行数据库查询并返回查询结果  
public ResultSet query(String sql){  
    try{  
        con = getcon();  
        stat = con.createStatement();  
        rs = stat.executeQuery(sql);  
    }catch(Exception ex){  
        ex.printStackTrace();  
    }  
    return rs;  
}  

//执行数据库更新
public void update(String sql){

try{

con = getcon();

stat = con.createStatement();

stat.executeUpdate(sql);

}catch(Exception ex){

ex.printStackTrace();

}

}

//执行数据库更新  
public void update(String sql,String[] args){  
    try{  
        con = getcon();  
        pstat = con.prepareStatement(sql);  
        for (int i=0;i<args.length;i++){  
            pstat.setString(i+1,args[i]);  
        }  
        pstat.executeUpdate();  
    }catch(Exception ex){  
        ex.printStackTrace();  
    }  
}

//关闭数据库连接  
public void close(){  
    try{  
        if (rs != null)rs.close();  
        if (stat != null)stat.close();  
        if (pstat != null)pstat.close();  
        if (con != null)con.close();  
    }catch(Exception ex){  
        ex.printStackTrace();  
    }         
}

}

这是userBean页面
package com.regist;
public class userBean {
String userName = null;
String userPwd = null;
boolean login = false;
public userBean(){

}
 public String getUserName() {  
        return userName;  
    }  
    public void setUserName(String userName) {  
        this.userName = userName;  
    }  
    public String getUserPwd() {  
        return userPwd;  
    }  
    public void setUserPwd(String userPwd) {  
        this.userPwd = userPwd;  
    }  
    public boolean isLogined() {  
        return login;  
    }  
    public void setLogined(boolean logined) {  
        this.login = logined;  
    }   

}

这是login登陆界面
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.DriverManager"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">


function cheek() { if(document.getElementsByName("userName").value==""){ alert("对不起,用户名不能为空值!"); return false; } if(document.getElementsByName("passWord").value==""){ alert("对不起,密码不能为空值!");return false; } return true; }




用户登陆



用户名:




密    码:




    <label>
        <input type="submit" value="提交" name="name">

    &nbsp;&nbsp;&nbsp;&nbsp;

        <input type="reset" value="重置" name="reset">

    &nbsp;&nbsp;&nbsp;&nbsp;

        <input type="button" value="注册" name="button">
    </label>




这是regist页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*"%>
<%@ page import="com.regist.dbBean" %>
<%@ page import="com.regist.userBean" %>
/jsp:useBean
/jsp:useBean

<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">


验证用户登录
<%
String userName = user.getUserName();

String userPwd = user.getUserPwd();
String sql = "select * from land where account='"+userName+"' and password='"+userPwd+"'";
if (user.isLogined()){

out.println("您已经登录过了!");

}else{
ResultSet rs = dbB.query(sql);
if (rs.next()){

user.setLogined(true);

out.println("恭喜您登录成功!");

}else{
user.setLogined(false);

out.println("对不起,您的帐号或密码不正确!");
}
    }
 %>


运行说是这里错了,各位大神帮帮忙,十分感谢
org.apache.jasper.JasperException: An exception occurred processing JSP page /regist.jsp at line 42

39: out.println("鎮ㄥ凡缁忕櫥褰曡繃浜�");

40: }else{
41: ResultSet rs = dbB.query(sql);
42: if (rs.next()){

43: user.setLogined(true);

44: out.println("鎭枩鎮ㄧ櫥褰曟垚鍔�");

45: }else{

1个回答

wojiushiwo945you
wojiushiwo945you   Ds   Rxr 2016.03.28 21:17

首先,你这个后台的dbBean 类的各个方法都经过测试正确么?
其次,你在后台设置下断点,看看是否有数据返回呢?

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!