1,现在我连接sqlserver数据库后,通过jtable显示数据,如何实现分页显示?
package com.cn.test1;
/*
* 功能:连接数据库,显示报表
* 作者:towne
* 日期:2017.03.01
*/
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
public class Demo2 extends JFrame {
Connection con;
ResultSet rs;
PreparedStatement ps;
JTable tab;
JScrollPane jsp;
Vector rowData,columNames;
String className="com.microsoft.jdbc.sqlserver.SQLServerDriver";
String url="jdbc:microsoft:sqlserver://127.0.0.1:1433;databaseName=db_database01";
String user="sa";
String pwd="";
public Demo2() {
try {
Class.forName(className);
con=DriverManager.getConnection(url,user,pwd);
//创建表头-列
columNames = new Vector();
columNames.add("编号");
columNames.add("用户");
columNames.add("密码");
//创建行数据
rowData = new Vector();
String sql="select * from tb_user";
ps = con.prepareStatement(sql);
rs = ps.executeQuery();
while(rs.next()) {
Vector row = new Vector();
row.add(rs.getString(1));
row.add(rs.getString(2));
row.add(rs.getString(3));
rowData.add(row);
}
//初始化数据表格
tab = new JTable(rowData,columNames);
tab.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);//
//初始化滚动面板
jsp = new JScrollPane(tab);
this.setTitle("报表显示");
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setBounds(100, 100, 700, 400);
this.getContentPane().add(jsp);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
public static void main(String[] args) {
Demo2 demo = new Demo2();
}
}