在java swing中使用滚动分页将数据库中的大型结果集显示为jtable [英] Show large resultset from database into jtable with scroll pagination in java swing
问题描述
大家好,
我对java很新,所以如果你认为这个问题很愚蠢,请忽略。我已经分配了一个任务,我必须从h2数据库表中提取数据,并从一个表中提取超过1亿的行(SAD)。**最后将数据显示到带有分页的aj表中。
我真正想要的是,当程序启动时,它会从数据库中提取1个lac记录,当用户向下滚动到第一个lac记录的末尾并再次从数据库中提取另一个lac数据时等等...... />
如果他向上滚动最好,程序不会提取已经提取的行。我希望我能很好地解释我的问题。
现在,经过大量的谷歌搜索,我发现这个(https://coderanch.com/t/345383/java/JTable-Paging)非常好做了我想要的90%的文章,但没有与数据库连接。任何人都可以告诉你如何做到这一点吗?
以下是示例代码:
我尝试过:
Hello all,
I am very new to java, so please ignore if you think this question is stupid. A task has been assigned to me in which I have to extract data from h2 database table with **more than 1 crore rows (SAD) from one table.** And finally show that data into a j table with pagination.
What i exactly want is that when the program starts, it extract lets say 1 lac record from database and when user scroll down to the end of first lac records and again extract another lac data from database and so on …
It will be best if he goes scroll up, the program doesn’t extract rows which are already extracted. I Hope I explained my problem here very well.
Now, After a lot of googling, I found this (https://coderanch.com/t/345383/java/JTable-Paging)very good article which does 90% of what I want, but not with database connection. Can any one tell how to do this ?
Here is the sample code :
What I have tried:
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;
}
}
}
推荐答案
打开数据库并获取数据很昂贵,所以通常是一组数据获取,存储在数据模型中而不是显示。我会根据速度获取至少两页的数据集。
某种类似的方式 example 通过获取所有数据并将其存储到普通模型数组(data)。
Open a database and fetch data is expensive, so normally is a bunch of data fetched, stored in the data model and than displayed. I would fetch data set for at least two pages depending on the speed.
A somehow similar example by fetching all data and storing it into the plain model array ("data").
这篇关于在java swing中使用滚动分页将数据库中的大型结果集显示为jtable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!