日期:2014-05-16  浏览次数:20401 次

随机抽取几条数据库记录的Java代码

其中的10表示抽取的记录数量,200000表示总记录数减去抽取记录数量的余值,其中rid为主键

public Vector getSomeReader() {
        Vector result = new Vector();
        try {
                Random r=new Random();                 
                PreparedStatement pstm = con
                                .prepareStatement("select top 10 rid from newreaderfull where rid not in(select top " +r.nextInt(200000)+ " rid from newreaderfull order by rid) order by rid");
                ResultSet res = pstm.executeQuery();
                while (res.next()) {
                        Vector sub = new Vector();
                        sub.add(res.getString(1));
                        result.add(sub);
                }
        } catch (Exception e) {
                System.out.println(e.getMessage());
        }
        return result;
}