Oracle常见的查询代码
/** * 分页查询 */ int currentPage=3;//当前页码 int pageSize=5;//每页的记录条数 String sql=" select * from " + "(select mid,name,age,birthday,note,rownum rn from member where rownum <=?)temp " + "where temp.rn>? "; PreparedStatement ps=conn.prepareStatement(sql); ps.setInt(1, currentPage*pageSize); ps.setInt(2, (currentPage-1)*pageSize); ResultSet rs = ps.executeQuery(); while(rs.next()){ System.out.println(rs.getInt(1)+"、"+rs.getString(2)+"、"+rs.getInt(3)+"" + "、"+rs.getDate(4)+"、"+rs.getString(5)); }
/** * 根据name字段进行模糊查询(部分代码) */ String column ="name"; String keyWord="李"; //"?"填充的占位符只有数据才可以使用,而对于列是无法使用的 String sql=" select mid,name,age,birthday,note from member where "+column+" like ? "; PreparedStatement ps=conn.prepareStatement(sql); ps.setString(1, "%"+keyWord+"%"); ResultSet rs = ps.executeQuery(); while(rs.next()){ System.out.println(rs.getInt(1)+"、"+rs.getString(2)+"、"+rs.getInt(3)+"" + "、"+rs.getDate(4)+"、"+rs.getString(5)); }
/** * 统计查询 */ String sql=" select count(*) from member" ; PreparedStatement ps=conn.prepareStatement(sql); ResultSet rs = ps.executeQuery(); if(rs.next()){ Long count=rs.getLong(1); System.out.println(count); }