Java GUI+mysql+分頁查詢

1.要求 : 建立一個學生信息管理數據庫java

2.實現分頁查詢mysql

代碼以下:sql

a)學生實體類:數據庫

/**
 * @author: Annie
 * @date:2016年6月23日
 * @description:學生實體類   學號+姓名+密碼
 */
public class student {
    private int sid;
    private String sname;
    private String spassword;

    public student(int sid, String sname, String spassword) {
        this.sid = sid;
        this.sname = sname;
        this.spassword = spassword;
    }

    public void setSid(int sid) {
        this.sid = sid;
    }

    public void setSname(String sname) {
        this.sname = sname;
    }

    public void setSpassword(String spassword) {
        this.spassword = spassword;
    }

    public int getSid() {
        return this.sid;
    }

    public String getSname() {
        return this.sname;
    }

    public String getSpassword() {
        return this.spassword;
    }

    public void showStudentInfo() {
        System.out.println("學號:" + this.getSid() + "\t姓名:" + this.getSname()
                + "\t密碼:" + this.getSpassword());
    }

    public String getStudentInfo() {
        return "學號" + this.getSid() + "姓名" + this.getSname() + "密碼:"
                + this.getSpassword()+"\n";
    }

}


b)數據庫操做類數組

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Iterator;


/**
 * @author: Annie
 * @date:2016年6月23日
 * @description:
 */
public class linkDB {
    
      ArrayList<student> arrayS = null;
        final int per_pages_size=5;//每一個頁面的長度,即每一個頁面幾條數據
        
        //數據庫的鏈接的方法
        public Connection getConnection2() {
            Connection conn = null;
            String url = "jdbc:mysql://localhost:3306/test";
            try {
                Class.forName("com.mysql.jdbc.Driver");
                conn = DriverManager.getConnection(url, "root", "12345678");
            } catch (SQLException e) {
                System.out.println("SQL 異常");
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                System.out.println("數據庫沒有找到");
                e.printStackTrace();
            }
            return conn;
        }

        
        /**
         * 數據庫中查詢的代碼,返回一個結果集
         * */
        public ResultSet getAllStudent_info() {
            Connection conn = this.getConnection2();
            Statement comm = null;
            ResultSet rs = null;
            try {
                comm = conn.createStatement();

                String sql2 = "select * from student order by sid";
                rs = comm.executeQuery(sql2);
                return rs;

            } catch (SQLException e) {
                e.printStackTrace();
            }
            return rs;
        }
        
        
      /**
       * 獲取數據庫中的總行數
       * */
        public int getPagesTotalSize() {
            //獲得查詢的結果集
            ResultSet rs = this.getAllStudent_info();
            int totalsize = 0;//數據整體的行數
            try {
                rs.last();  // 將光標移動到此 ResultSet 對象的最後一行
              //  獲取當前行編號(先將光標移到最後一行,而後再獲取最後一行的下標,便可獲得整個數據庫的行數)
                totalsize = rs.getRow();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return totalsize;
        }
        
        /**
         * 獲得總共的頁數的方法
         * */
        public int getPagesNum() {
            int totalsize = (Integer)this.getPagesTotalSize()/per_pages_size;   
            return (totalsize+1);
        }
        
        
        /**
         * 將從數據庫裏遍歷到的數據裝到數組裏
         * */
        public ArrayList<student> get_per_Page(int per_pages_num) {
            //獲得全部學生的結果集
            ResultSet rs = this.getAllStudent_info();
            //定義一個裝學生對象的集合
            ArrayList<student> arrayS = new ArrayList();
            int totalsize = this.getPagesTotalSize();
            
            int sid = 0;//學號
            String sname = null;//姓名
            String spassword = null;//密碼
            
            if (per_pages_size * (per_pages_num - 1) < totalsize) {
                int start = per_pages_size * (per_pages_num - 1) + 1;
                int end = 0;
                if (per_pages_size * per_pages_num > totalsize) {
                    end = totalsize;
                } else {
                    end = per_pages_size * per_pages_num;
                }
                for (int i = start; i <= end; i++) {
                    try {
                        rs.absolute(i);
                        sid = rs.getInt(1);
                        sname = rs.getString(2);
                        spassword = rs.getString(3);
                        student s = new student(sid, sname, spassword);
                        arrayS.add(s);
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
            } else {
                System.out.println("超出範圍");
            }
            return arrayS;
        }

        
        /**
         * 展現數據的方法
         * */
        public void test_per_page(int id) {
            ArrayList<student> arrayS = this.get_per_Page(id);

            //遍歷數組,將數據展現在文本域裏
            for (Iterator i = arrayS.iterator(); i.hasNext();) {
                student s = (student) i.next();
                s.showStudentInfo();
            }
        } 
}

c)主界面類app

import java.awt.BorderLayout;

import java.awt.Button;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.Iterator;

/**
 * @author: Annie
 * @date:2016年6月23日
 * @description:
 */
public class MainPages  extends Frame{

    private int currentlyPage = 1; //初始化當前的頁數
    private Panel center, bottom;
    private Button previously, next, first, tail;
    private TextArea ta;
    private linkDB ldb = new linkDB();
    ArrayList<student> arrayS = null;

    public MainPages() {

        this.previously = new Button("上一頁");
        this.next = new Button("下一頁");
        this.first = new Button("首頁");
        this.tail = new Button("尾頁");
        this.ta = new TextArea();
        ta.setRows(5);
        ta.setBounds(0, 0, 40, 20);
        this.initialization();

        this.center = new Panel(new GridLayout(1, 1));
        this.bottom = new Panel(new GridLayout(1, 4));

        center.add(ta);

        bottom.add(previously);
        bottom.add(next);
        bottom.add(first);
        bottom.add(tail);

        /*對首頁、下一頁、上一頁、尾頁作監聽*/
        MyListener ml = new MyListener();
        this.previously.addActionListener(ml);
        this.next.addActionListener(ml);
        this.first.addActionListener(ml);
        this.tail.addActionListener(ml);

        // this.add(center,BorderLayout.NORTH);不知是何緣由?這樣的話,顯示不bottom。可能和BorderLayout有關係。
        this.add(center, BorderLayout.CENTER);
        this.add(bottom, BorderLayout.SOUTH);

        this.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                setVisible(false);
                dispose();
                System.exit(0);
            }
        });//對關閉窗口作監聽

        this.setBounds(200, 200, 350, 180);
        this.setVisible(true);

    }

    /**
     * 初始化第一頁
     * */
    void initialization() {
        showDate();
    }

    /**
     * 對按鈕作監聽的方法
     * */
    class MyListener implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            ta.setText("");
            if (e.getSource() == previously) {//上一頁的按鈕
                if (currentlyPage >= 2) {
                    currentlyPage--;
                } else {
                    ta.append("當前爲第一頁!\n");
                }
                //展現數據
                showDate();


            } else if (e.getSource() == next) { //若是爲下一頁
                ta.setText("");
                int pagesNum = ldb.getPagesNum();
                if (currentlyPage < pagesNum) {//若是當前的頁碼小於總頁數
                    currentlyPage++;
                } else {
                    ta.append("當前爲最後一頁!\n");
                }

                showDate();

            } else if (e.getSource() == first) {//若是是首頁
                ta.setText("");
                currentlyPage = 1;
                showDate();


            } else if (e.getSource() == tail) {//若是是尾頁
                ta.setText("");
                currentlyPage = ldb.getPagesNum();
                showDate();
            }
        }
    }

    /**
     * 展現數據
     */
    public void showDate() {
        arrayS = ldb.get_per_Page(currentlyPage);
        Iterator i = arrayS.iterator();
        while ( i.hasNext()) {
            student s = (student) i.next();
            ta.append(s.getStudentInfo());
        }
    }
    public static void main(String[] args) {
        MainPages mp = new MainPages();
    }
}

 

d)建立數據庫的表格及插入數據this

create table student
(sid int not null,
sname varchar(50),
spassword varchar(20),
primary key(sid)
);

insert into student values('1','科比','24');
insert into student values('2','加內特','5');
insert into student values('3','艾弗森','23');

insert into student values('11','德羅巴','24');
insert into student values('12','羅尼','5');
insert into student values('13','克里斯蒂亞諾 諾那爾多','23');
insert into student values('21','梅西','24');
insert into student values('22','比利亞','5');
insert into student values('23','哈維','23');
insert into student values('31','麥孔','24');
insert into student values('32','埃弗拉','5');
insert into student values('33','費爾南德斯','23');
insert into student values('41','維迪奇','24');
insert into student values('42','卡西利亞斯','5');

insert into student values('43','詹姆斯','23');
insert into student values('51','韋德','24');
insert into student values('52','姚明','5');
insert into student values('53','鄧肯','23');url

 

實現效果圖:spa

相關文章
相關標籤/搜索