request.setCharacterEncoding("UTF-8");
     String uname = request.getParameter("uname");
     String pwd = request.getParameter("pwd");
     Connection con = null;
     PreparedStatement ps = null;
     ResultSet rs = null;
     final String DRIVER="com.microsoft.sqlserver.jdbc.SQLServerDriver";
     final String UNAME="sa";
     final String PWD = "sa";
     final String URL = "jdbc:sqlserver://127.0.0.1:1433;databaseName=test";
     String sql = "select count(*) from users where uname=? and pwd=?";
     boolean flag = false;
     try{
     Class.forName(DRIVER);
     con = DriverManager.getConnection(URL,UNAME,PWD);
     ps = con.prepareStatement(sql);
     ps.setString(1,uname);
     ps.setString(2,pwd);
     rs = ps.executeQuery();
     rs.next();
     int count =rs.getInt(1);
     if(count>0){
     flag = true;
     } 
     }catch(Exception ex){
     ex.printStackTrace();
     }finally{
     try{
     if(rs!=null) rs.close();
     if(ps!=null) ps.close();
     if(con!=null) con.close();
     }catch(Exception ex){
     ex.printStackTrace();
     }
     }
     if(flag){
     session.setAttribute("login","true");
     request.getRequestDispatcher("success.jsp").forward(request,response);
     }else{
     request.setAttribute("error"," error,check your name and pwd,login again");
     request.getRequestDispatcher("login.jsp").forward(request,response);
     }
是哪一句将提交的数据和数据库比较后然后判断的

解决方案 »

  1.   

    This one
      rs = ps.executeQuery(); //运行数据库查询
      rs.next();
      int count =rs.getInt(1);
      
      if(count>0){ //这个来判断查询是否有结果
      flag = true;
      }  
    //Ali
      

  2.   

    String sql = "select count(*) from users where uname=? and pwd=?";
    这句就是说查找所输入用户名、密码的记录
    搜出来了就是对了,没有结果就是不对
      

  3.   

    供LZ参考 
    /**
     * 用于验证用户登录
     * 
     * @author Jeelon
     * @param userName
     *            :登录用户名
     * @param password
     *            :登录密码
     */
    public boolean checkUser(String name, String password) {
    boolean bool = false;
    try { connect = new ConnectDB().getConnect();
    ps = connect
    .prepareStatement("select userPassword from users where userName=?");
    ps.setString(1, name);
    result = ps.executeQuery();
    if (result.next()) {// 用户存在
    if (result.getString(1).trim().equals(password)) {// 如果密码正确
    bool = true;
    } else {
    bool = false;// 密码不正确
    }
    } else {
    bool = false;// 用户都不存在
    } } catch (Exception e) {
    e.printStackTrace();
    // TODO: handle exception
    } finally {
    this.close();
    }
    return bool;
    }
      

  4.   

    count就是验证后返回的个数,要是为0就不对
      

  5.   

    int count =rs.getInt(1);
      if(count>0){
      flag = true;
      }  
    如果flag为true,则说明登录成功
      

  6.   

    int count =rs.getInt(1);
      if(count>0){
      flag = true;
      }  
    这个地方。
      

  7.   


    package base;import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.List;public class BaseDao {
    private static final String DRIVER="com.microsoft.sqlserver.jdbc.SQLServerDriver";
    private static final String URL = "jdbc:sqlserver://127.0.0.1:1433;databaseName=test";
    private static final String USER = "sa";
    private static final String PWD = "ok";

    private ResultSet resultSet;
    private PreparedStatement preparedStatement;
    private Statement statement;
    private Connection connection;


    /**
     * 开启
     * @throws ClassNotFoundException
     * @throws SQLException
     */
    public void open() throws ClassNotFoundException, SQLException 
    {
    Class.forName(DRIVER);
    connection = DriverManager.getConnection(URL,USER,PWD);
    }


    /**
     * 关闭数据库
     *
     */
    public void close() 
    {
    if (null != this.connection) 
    {
    try {
    this.connection.close();
    } catch (SQLException e) {
    e.printStackTrace();
    }
    }
    if (null != this.preparedStatement) 
    {
    try {
    this.preparedStatement.close();
    } catch (SQLException e) {
    e.printStackTrace();
    }
    }
    if (null != this.statement) 
    {
    try {
    this.statement.close();
    } catch (SQLException e) {
    e.printStackTrace();
    }
    }
    if (null != this.connection) 
    {
    try {
    this.connection.close();
    } catch (SQLException e) {
    e.printStackTrace();
    }
    }

    }


    public int executeSql(String sql) throws SQLException 
    {
    this.statement = this.connection.createStatement();
    return this.statement.executeUpdate(sql);
    }

    public int executeSql(String sql,List list) throws SQLException 
    {
    this.preparedStatement = this.connection.prepareStatement(sql);
    if (null != list)
    {
    for (int i =0; i< list.size(); i++) 
    {
    this.preparedStatement.setString(i+1, list.get(i).toString());
    }
    }
    return this.preparedStatement.executeUpdate();
    }



    public ResultSet executeUpdate(String sql) throws SQLException 
    {
    this.statement = this.connection.createStatement();
    this.resultSet = this.statement.executeQuery(sql);
    return this.resultSet;
    }

    public ResultSet executeUpdate(String sql,List list) throws SQLException 
    {
    this.preparedStatement = this.connection.prepareStatement(sql);
    if (null != list)
    {
    for (int i =0; i< list.size(); i++) 
    {
    this.preparedStatement.setString(i+1, list.get(i).toString());
    }
    }
    this.resultSet = this.preparedStatement.executeQuery();
    return this.resultSet;
    }
    }
      

  8.   

     rs.next();
    这是判断什么
      

  9.   

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title>利用正则表达式进行表单的简单验证</title>
    </head>
    <script language="javascript">
    function check(){
    var name = document.getElementById("UserName").value;
    var pwd = document.getElementById("UserPasse").value;
    var repwd = document.getElementById("UserREPasse").value;
    var birth = document.getElementById("UserBirthday").value;
    var age = document.getElementById("Userage").value;
    var email = document.getElementById("userEmail").value;
    var namePattern = /^\s*$/;
    var pwdPattern = /^.{6,12}$/;  // 密码长度要求为6-12位
    var birthPattern = /^19\d{2}-(0[1-9]|1[0-2])-(0[1-9]|[1-2]\d|3[0-1])$/;
    var emailPattern = /^\w+@{1}\w+\.\w+$/;
    //邮箱不能以@开头,且@只能出现一次,不能以.结束,且@和.之间至少有一个字符
    if(namePattern.test(name)){
    alert("用户名不能为空!");
      return false;
    }
    if(!pwdPattern.test(pwd)){
    alert("密码长度不符合要求,应为:(6-12位)");
      return false;
    }
    if(repwd!=pwd){
    alert("两次密码输入不一致!");
      return false;
    }
    if(!birthPattern.test(birth)){
    alert("生日不符合规范,需要的格式为:YYYY-MM-DD");
      return false;
    }
    if(!(age>0&&age<=100)){
    alert("年龄应在1-100之间");
      return false;
    }
    if(!emailPattern.test(email)){
    alert("邮箱不符合规范,规范邮箱:[email protected]");
    return false;
    }
    return true;
    }
    </script>
    <body >
    <h4>用户注册页面</h4>
    <form name="form1" action="suc.html" method="post" onsubmit="return check();">
    <table border="1" width="400" >
    <tr>
    <td height="35">用户名:</td>
    <td><input type="text" name="UserName" id="UserName"></td>
    </tr>

    <tr>
    <td height="35">密码:</td>
    <td><input type="password" name="UserPasse" id="UserPasse"></td>
    </tr>

    <tr>
    <td height="35">确认密码:</td>
    <td><input type="password" name="UserREPasse" id="UserREPasse"></td>
    </tr>

    <tr>
    <td height="35">生日:</td>
    <td><input type="text" name="UserBirthday" id="UserBirthday"></td>
    </tr>

    <tr>
    <td height="35">性别:</td>
    <td>
    <input type="radio" name="sex" id="sex" value="男" checked>男
    <input type="radio" name="sex" id="sex" value="女" >女
    </td>
    </tr>

    <tr>
    <td height="35">年龄:</td>
    <td><input type="text" name="Userage" id="Userage"></td>
    </tr>

    <tr>
    <td height="35">邮件地址:</td>
    <td><input name="userEmail" id="userEmail" type="text"/></td>
    </tr>


    <tr>
    <td colspan="2">
    <input type="submit" name="submitButton" id="submitButton" value="提交">
    <input type="reset" name="resetButton" id="resetButton">
    </td>
    </tr>
    </table></form>
    </body>
    </html>
      

  10.   

    这是刚学完html和javascript时候写得
    希望对你有用,呵呵