/** * JDBC数据库分页查询 * @param sql 传入的sql语句 * @param startNo 起始页 * @param maxCount 每页取多少条记录 */ public ArrayList getDataFromAll(String sql,int startNo,int maxCount){ Connection conn=null; PreparedStatement pstat=null; ResultSet rs=null; ArrayList list=new ArrayList(); HashMap map=new HashMap(); if(startNo==0) { startNo=1; } pstat = conn.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY); rs = pstat.executeQuery(); //取得数据表中的字段数目,类型等返回结果 ResultSetMetaData rsmd=rs.getMetaData(); //列的总数 int columnCount = rsmd.getColumnCount(); pstat.setMaxRows(startNo*maxCount); rs = pstat.executeQuery(); //将游标移动到第一条记录 rs.first(); // 游标移动到要输出的第一条记录 rs.relative(((startNo-1)*maxCount)-1); while(rs.next()){ map=new HashMap(); for(int y=1;y <=columnCount;y++){ map.put(rsmd.getColumnName(y), rs.getObject(y)); } list.add(map); } } catch (SQLException e) { e.printStackTrace(); }finally{ //如果执行完毕还存在数据库连接则关闭它 try { if(conn!=null) { conn.close(); } if(pstat!=null) { pstat.close(); } if(rs!=null) { rs.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return list; }