日期:2014-05-17  浏览次数:20808 次

一个关于数据库取值的问题
我在做一个考试系统,现在做到判断你有无考过这一块,我的方法是从数据库中取出值来后
Java code
 public boolean ifmathexamed(String usernumber)
 { 
   boolean flag=true;
   PreparedStatement pstmt=null;
   ResultSet rs=null;
   Connection conn=null;
   conn=Getconnect.getconnection();
   String sql="select mathscore form userinfomation where usernumber=?";
   try {
   pstmt=conn.prepareStatement(sql);
   pstmt.setString(1, usernumber);
   rs=pstmt.executeQuery();
   
   if(rs.next())
   {
    
     flag=false;
   }


这里我的想法是,如果数据库中已经存放了考试分数,说明你已经考过了,所以flag=false;然后在servlet中调用这个方法,如果为false则跳转到一个我制定的错误页面,告知已考过了,我数据库中放了两个考生,001和002,001总有分数,002中没有,那数据库就自动为null,但是我用这个办法运行后,它两个都给我跳转到制定的错误页面,这是为什么呀,我上面的usernumber是开始登入就保存在session中的,然后通过这个String usernumber=(String) session.getAttribute("usernumber");方法转换成String类型的,麻烦各位了


------解决方案--------------------
当然有记录了 你的sql应该过滤掉 分数为空或为null的记录
比如 "select mathscore form userinfomation where mathscore<>'' and matscore<>null and usernumber=?"
------解决方案--------------------
String sql="select count(*) form userinfomation where usernumber=? and mathscore is null";

------解决方案--------------------
判断时候,有值才会下去 啊,为什么你还搞成事false啊