您的位置:首页 > 数据库

JAVA+Swing +sql 分页代码

2010-05-21 10:36 381 查看
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.AbstractTableModel;
public class PageTest extends JFrame implements MouseListener,ActionListener{
public final String[] columnHeaders = {"ID(编号)","Nmae(姓名)","Sex(性别)" };
JTable ab;
JLabel ab1,ab2,ab3,ab4;
JTextField test1,test2,test3,test01,test02,test03,test04;
JButton bu,bu1,bu01,bu02,bu03,bu04;
JScrollPane jsp;
public String[][] str = null;
ShuZhu sz = new ShuZhu();
int y =0;
public PageTest(){
sz.sqlCont();
setTitle("------------------");
setBounds(100,100,500,400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(null);
bu = new JButton("下一页");
bu.setBounds(415,10,75,30);
bu.addActionListener(this);
bu1 = new JButton("上一页");
bu1.setBounds(415,50,75,30);
bu1.addActionListener(this);
ShuZhu sz = new ShuZhu();
str = sz.selectSql(3,y);
testChn tc = new testChn();

ab = new JTable(tc);
jsp = new JScrollPane(ab);
jsp.setBounds(10,10,400, 80);
ab.addMouseListener(this);
test1 = new JTextField(20);
test1.setBounds(20,100,100,35);
test2 = new JTextField(20);
test2.setBounds(170,100,100,35);
test3 = new JTextField(20);
test3.setBounds(320,100,100,35);
add(test1);
add(bu);
add(bu1);
add(test2);
add(test3);
add(jsp);
setVisible(true);
}
public static void main(String[] args) {
new PageTest();
}
public void mouseClicked(MouseEvent e) {
test1.setText((String)ab.getValueAt(ab.getSelectedRow(),0));
test2.setText((String)ab.getValueAt(ab.getSelectedRow(),1));
test3.setText((String)ab.getValueAt(ab.getSelectedRow(),2));
}
public void actionPerformed(ActionEvent e) {
int x = sz.sqlCont();
if(e.getActionCommand().equals("下一页")){
y++;
if(y>x/3){
JOptionPane.showMessageDialog(this,"没有下一页了");
y--;
}else{
str = sz.selectSql(3, y);
ab.updateUI();
}
}else if(e.getActionCommand().equals("上一页")){
y--;
if(y<0){
JOptionPane.showMessageDialog(this,"没有上一页了");
y++;
}else{
str = sz.selectSql(3, y);
ab.updateUI();
}
}
}
public class testChn extends AbstractTableModel{
public String getColumnName(int col){
return columnHeaders[col];
}
public int getColumnCount() {
return str[0].length;
}
public int getRowCount() {
return str.length;
}
public Object getValueAt(int row,int column){
return str[row][column];
}
}
---------------------------------------------------------------------------------

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.table.AbstractTableModel;

public class ShuZhu{
public static String[][] dataValues =null;
public Connection connSql(){
Connection conn = null;
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
conn = DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;databaseName=strBT","sa","") ;
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
public int sqlCont(){
int x = 0;
try {
Connection conn = connSql();
String sql = "select* from stu1";
Statement sta = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
ResultSet re = sta.executeQuery(sql);
re.last();
x = re.getRow();
} catch (SQLException e) {
e.printStackTrace();
}
return x;
}
public String[][] selectSql(int x,int y){
dataValues = new String[x][3];
int count = 0 ;
Connection conn = connSql();
String sql = "select top "+x+" * from stu1 where id not in (select top "+y*3+" id from stu1 order by id )order by id";
try {
Statement sta = conn.createStatement();
ResultSet re = sta.executeQuery(sql);
while(re.next()){
dataValues[count][0]=String.valueOf((re.getInt(1)));
dataValues[count][1]=re.getString(2);
dataValues[count][2]=re.getString(3);
count++;
}
} catch (SQLException e) {
e.printStackTrace();
}
return dataValues;
}
}

转:http://blog.sina.com.cn/s/blog_5f06ab450100ctfn.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: