import java.sql.*;
import java.io.*;
import java.net.*;
public class Login
{
  public static boolean loginCheck(DataInputStream dis)
  {
    int id = -1;
    String password = "";
    String passCheck = null;    String SQL_Query;
    ResultSetTableModel tableModel;    try {      id = dis.readInt();
      password = dis.readUTF();      System.err.println("Login Authentication request from " + id
                         + " with password " + password);      // SQL Query for inserting the content
      SQL_Query = "SELECT password FROM " + Parameters.REG_TABLE
          + " WHERE id = " + id;      System.err.println("LoadList()方法开始查询");      // update the query
      tableModel = new ResultSetTableModel(SQL_Query, "execute");      ResultSet result = tableModel.getResultSet();      System.err.println("ResultSet successfully queried");      while(result.next())
      {
        passCheck = result.getString(1);
      }      System.err.println("PassCheck got : " + passCheck);
      //close connection
      tableModel.disconnectFromDatabase();
    }
    catch (SQLException e) {
      System.out.println("SQLException");
    }
    catch (ClassNotFoundException e) {
      System.out.println("ClassNotFoundException");
    }
    catch (IllegalAccessException e) {
      System.out.println("IllegalAccessException");
    }
    catch (InstantiationException e) {
      System.out.println("InstantiationException");
    }
    catch (IllegalStateException e) {
      System.out.println("IllegalStateException");
    }
    catch(IOException ioe) {
      System.err.println("IOException " + ioe);
    }    // compare the queried password with the received password
    if( password.equals(passCheck) && password != "" && passCheck != null)
    {
      System.err.println("Password the same: Check successful!");
      return true;
    }
    else
    {
      System.err.println("Password not the same: Check unsuccessful!");
      return false;
    }
  }
}

解决方案 »

  1.   

    //晕,用得着那么麻烦吗?
    //借用楼上的,替你改一个String sqlStr = " select * from "+myTableName+"  where id = ? and password = ? ";
    //.........取得Connection con,id,password
    PreparedStatement ps = con.preparedStatement( sqlStr);
    ps.setInt(1,id);
    ps.setString(2,id);
    ResultSet rs = ps.executeQuery();
    if( rs.next()) System.out.println(" 你的密码是正确的");
    else System.out.println(" 你的密码是错误的");
    //.................记得关闭连接剩下的内容自己组装去,其实password里面允许有',&等不规则字符
      

  2.   

    先说这么不好使再发给我,好不好?
    [email protected]
      

  3.   

    I want to get one too
    email:[email protected]
      

  4.   

    to Bruce_1981 (许真) 
    怎么又是你呀。最近总看见你再问问题
    好像你要做的东西和我差不多呀不过我就是没有做数据库验证的部分
    觉得好麻烦呀关注一下