public boolean Compare(){
     Connection con = null;
     PreparedStatement ps = null;
     ResultSet rs=null;     boolean exist=false;
       try{
         con = DriverManager.getConnection(url);
         String selectStr = "select usermax,usernum from userlimit";
         ps = con.prepareStatement(selectStr);
         rs=ps.executeQuery();
         if(usermax>usernum)
           exist=true;
            System.out.println("比较");
       }
       catch(SQLException exc){
          exc.printStackTrace();
       }
      return exist;
   }
}
我想实现usermax>usernum的时候就返回真,否则为假,不知道上面哪里有问题,谢谢了。

解决方案 »

  1.   

    在上面的程序中,你得到了resultSet,但是没有使用它。同时usermax和usernum两个参数没有定义。
    编译的时候就通不过。建议楼主看看ResultSet的API,之后就会明白了。
      

  2.   

    public boolean Compare(){
         Connection con = null;
         PreparedStatement ps = null;
         ResultSet rs=null;     boolean exist=false;
           try{
             con = DriverManager.getConnection(url);
             String selectStr = "select usermax,usernum from userlimit";
             ps = con.prepareStatement(selectStr);
             rs=ps.executeQuery();
             // ------------------------------
              int usermax, usernum;          if (rs.next()) {
                  usermax = rs.getInt("usermax");
                  usernum = rs.getInt("usernum");
                  if (usermax > usernum)
                      exist = true;
                }                    exist=true;
                System.out.println("比较");
           }
           catch(SQLException exc){
              exc.printStackTrace();
           }
          return exist;
       }
    }
    // 而且以上代码,既然没有使用参数就不要PrepareStatement 
    // 增加开销不是。Statement类已足够满足你的要求了。