JAVA+Swing +sql 分页代码

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

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值