222wan

导航

mysql语句查询最大值并获取

public int exadd(exam exam) throws Exception {
            
            Connection connection = DBUtil.dbCon();
           
            String sql="select max(eid) eid from juan";
            PreparedStatement pstmt = null;
             
            pstmt =connection.prepareStatement(sql);
          //查询到最大的试卷id然后增加
          ResultSet rs=pstmt.executeQuery();
          if(rs.next()) {
            int eid=rs.getInt("eid");
            
            eid++;
            
          
                    pstmt=null;
                    rs=null;
             sql = "insert into juan(eid,type,num,count,time,uid) value(?,?,?,?,?,?)";
                     
                       pstmt =connection.prepareStatement(sql);
               
             
          
            pstmt.setInt(1, eid);
            pstmt.setInt(2, exam.getType());
            pstmt.setInt(3, exam.getNum());
            pstmt.setInt(4, exam.getCount());
            pstmt.setInt(5, exam.getTime());
            pstmt.setString(6, exam.getUid());
           
            
            
            
            int count = pstmt.executeUpdate();
                //关闭数据库连接关闭访问reader
            DBUtil.close(connection);
            DBUtil.close(pstmt);
            DBUtil.close(rs);

            return count;
            }
          return 0;
        }

}

 必须下面加上if(rs.next ())然后if里面写其他的语句不然就会错记得关闭数据库连接rs这里是用来获得查询的

 

posted on 2023-11-28 21:22  角落的蘑菇  阅读(34)  评论(0编辑  收藏  举报