下面是我刚做的一个方法,我想把用户输入的帐号和密码是否存在于sql中来check该用户是否有登陆的权限.不懂的地方我用红色标记出来了,新手请高手门帮助 ...
        
        String userName="";
String userP=""; public   boolean userInfoLink(){
String strDBriver = "com.mysql.jdbc.Driver";
// 数据库ServiceID
String strConUrl="jdbc:mysql://127.0.0.1/user";
// 连接用户
String strUser="root";
// 连接密码
String strPass="";
Connection con = null;
Statement stmt = null;
ResultSet rs=null;
try{
Class.forName(strDBriver);
con = DriverManager.getConnection(strConUrl,strUser,strPass);
stmt = con.createStatement();
rs = stmt.executeQuery("select ? from USERINFO  ");
String strSelectSql="SELECT NICKNAME FROM userinfo WHERE  USERID=? AND PASSWORD=? ; ";
PreparedStatement pstmt=con.prepareStatement(strSelectSql);
pstmt.setString(1,userName);
pstmt.setString(2,userP );
                        //此处想用读进去的uservname和userp和sql进行连接来比较是否存在,不知道用什么方法来调用``
if(pstmt.)){
return true;
}else{
return false;
}


}catch(Exception e){
System.out.println(e);
}finally{
try{
if(rs!=null)
rs.close();
if(stmt!=null)
stmt.close();
if(con!=null)
con.close();
}catch(Exception e1){
System.out.println(e1);
}
}
}
谢谢拉.

解决方案 »

  1.   

    可以是下while(rs.next())
    如果有记录就说明名字和密码。ID匹配了
    如果rs没有找到相关记录就为false
      

  2.   

    不是很清楚你说的意思
    用rs=pstmt.executeQuery()执行查询,判断结果集rs里有没有纪录就行吧
      

  3.   

      String userName = "";  String userP = "";  public boolean userInfoLink() {
        String strDBriver = "com.mysql.jdbc.Driver";
        // 数据库ServiceID
        String strConUrl = "jdbc:mysql://127.0.0.1/user";
        // 连接用户
        String strUser = "root";
        // 连接密码
        String strPass = "";
        Connection con = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
          Class.forName(strDBriver);
          con = DriverManager.getConnection(strConUrl, strUser, strPass);
          String strSelectSql = "SELECT   NICKNAME   FROM   userinfo   WHERE     USERID=?   AND   PASSWORD=?   ;   ";
          PreparedStatement pstmt = con.prepareStatement(strSelectSql);
          pstmt.setString(1, userName);
          pstmt.setString(2, userP);
          rs = pstmt.executeQuery(); // 查询结果
          if (rs.next()) {
            return true;
          } else {
            return false;
          }
        } catch (Exception e) {
          System.out.println(e);
          return false; // 增加了返回值
        } finally {
          try {
            if (rs != null)
              rs.close();
            if (stmt != null)
              stmt.close();
            if (con != null)
              con.close();
          } catch (Exception e1) {
            System.out.println(e1);
          }
        }
      }
      

  4.   

      /**
       * 检查用户是否正确
       * 
       * @param userName 用户名
       * @param userP 密码
       * @return 如果都正确,返回true,否则返回false
       */
      public boolean userInfoLink(String userName, String userP) {
        String strDBriver = "com.mysql.jdbc.Driver";
        // 数据库ServiceID
        String strConUrl = "jdbc:mysql://127.0.0.1/user";
        // 连接用户
        String strUser = "root";
        // 连接密码
        String strPass = "";
        Connection con = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
          Class.forName(strDBriver);
          con = DriverManager.getConnection(strConUrl, strUser, strPass);
          String strSelectSql = "SELECT NICKNAME FROM userinfo WHERE  USERID=? AND PASSWORD=?";
          PreparedStatement pstmt = con.prepareStatement(strSelectSql);
          pstmt.setString(1, userName);
          pstmt.setString(2, userP);
          rs = pstmt.executeQuery();
          if (rs.next()) {
            return true;
          } else {
            return false;
          }
        } catch (Exception e) {
          System.out.println(e);
          return false;
        } finally {
          try {
            if (rs != null)
              rs.close();
            if (stmt != null)
              stmt.close();
            if (con != null)
              con.close();
          } catch (Exception e1) {
            System.out.println(e1);
          }
        }
      }