TEL:400-8793-956
当前位置:程序、服务器

在Java swing中使用滚动分页将数据库中的大结果集显示到jtable中

提问者: 近期获赞: 浏览人数: 发布时间:2021-01-30 13:23:16

 问:我是Java的新手,所以如果您认为这个问题很愚蠢,请忽略。已经分配给我一个任务,其中我必须从**一个表中的**超过1千万行(SAD)的h2数据库表中提取数据。**最后将数据分页显示到j表中。

我真正想要的是,当程序启动时,它会从数据库中提取1条lac记录,而当用户向下滚动至第一条lac记录的末尾并再次从数据库中提取另一条lac数据时,依此类推……
最好向上滚动,该程序不会提取已经提取的行。我希望我在这里很好地解释了我的问题。
现在,经过大量的搜索之后,我发现这篇非常出色的文章(https://coderanch.com/t/345383/java/JTable-Paging)可以完成我想要的90%的内容,但是与数据库连接无关。谁能告诉我该怎么做?
这是示例代码:
 
public class PagingTableModel extends AbstractTableModel {
 private static final int MAX_PAGE_SIZE = 1000000;
 private static final int LATENCY_MILLIS = 1500;
 private int dataOffset = 0;
 private ArrayList<Integer> data = new ArrayList<Integer>();
 private SortedSet<Segment> pending = new TreeSet<Segment>();
 public int getColumnCount() {
  return 2;
 }
 public String getColumnName(int col) {
  if (col == 0)
   return "ANUMBER";
  if (col == 1)
   return "BNUMBER";
  return "DATETIME";
 }
 public int getRowCount() {
  return 10000000; // one million
 }
 public Object getValueAt(int row, int col) {
  ArrayList<Integer> page = data;
  int pageIndex = row - dataOffset;
  //JOptionPane.showMessageDialog(null, dataOffset);
  if (pageIndex < 0 || pageIndex >= page.size()) {
   System.out.println("object at " + row + " isn't loaded yet");
   schedule(row);
   return "..";
  }
  Object rowObject = page.get(pageIndex);
  return rowObject;
 }
 private void schedule(int offset) {
  if (isPending(offset)) {
   return;
  }
  int startOffset = Math.max(0, offset - MAX_PAGE_SIZE / 2);
  int length = offset + MAX_PAGE_SIZE / 2 - startOffset;
  load(startOffset, length);
 }
 private boolean isPending(int offset) {
  int sz = pending.size();
  if (sz == 0)
   return false;
  if (sz == 1) {
   Segment seg = pending.first();
   return seg.contains(offset);
  }
  Segment lo = new Segment(offset - MAX_PAGE_SIZE, 0);
  Segment hi = new Segment(offset + 1, 0);
  for (Segment seg : pending.subSet(lo, hi)) {
   if (seg.contains(offset))
    return true;
  }
  return false;
 }
 private void load(final int startOffset, final int length) {
  final Segment seg = new Segment(startOffset, length);
  pending.add(seg);
  Runnable fetch = new Runnable() {
   public void run() {
    try {
     Thread.sleep(LATENCY_MILLIS);
    } catch (InterruptedException ex) {
     System.out.println("error retrieving page at " + startOffset + ": aborting");
     pending.remove(seg);
     return;
    }
    final ArrayList<Integer> page = new ArrayList<Integer>();
    for (int j = 0; j < length; j += 1) {
     page.add(new Integer(j + startOffset));
    }
    SwingUtilities.invokeLater(new Runnable() {
     public void run() {
      System.out.println("** loaded " + startOffset + " through " + (startOffset + length - 1));
      setData(startOffset, page);
      pending.remove(seg);
     }
    });
   }
  };
  new Thread(fetch).start();
 }
 private void setData(int offset, ArrayList<Integer> newData) {
  // This method must be called from the event dispatch thread.
  int lastRow = offset + newData.size() - 1;
  dataOffset = offset;
  data = newData;
  fireTableRowsUpdated(offset, lastRow);
 }
 public static void main(String[] argv) {
  JTable tab = new JTable(new PagingTableModel());
  JScrollPane sp = new JScrollPane(tab);
  JFrame f = new JFrame("PagingTableModel");
  f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  f.setContentPane(sp);
  f.setBounds(30, 50, 1300, 600);
  f.setVisible(true);
 }
 static final class Segment implements Comparable<Segment> {
  private int base = 0, length = 1;
  public Segment(int base, int length) {
   this.base = base;
   this.length = length;
  }
  public boolean contains(int pos) {
   return (base <= pos && pos < base + length);
  }
  public boolean equals(Object o) {
   return o instanceof Segment && base == ((Segment) o).base && length == ((Segment) o).length;
  }
  public int compareTo(Segment other) {
   int d = base - other.base;
   if (d != 0)
    return d;
   return length - other.length;
  }
 }
}
 
 
答:现在从你的代码里面看是没有问题,希望你在检查一下。
上一篇: 如何在台球桌球游戏中将点点射线从球棒传递到球
下一篇: fPDF版本问题