java代码笔记2010-06-24:java 实现一个分页操作(JDBC+GUI->AWT) 实例

一,完成了 数据内部的 分页操作
二,实现GUI 窗口。
1,按钮:首页 下一页 上一页 尾页
2,textarea 分页显示。每页显示5个数据。

注意点:
1,三个类。
2,我的mysql user=root password=root,
配置文件的路径:inputFile = new FileInputStream("src/test20100624_pages/p2.properties");
要根据实际情况进行修改。
大家可以根据自己的情况修改。

知识点:
一,配置文件:
Properties p2 = new Properties();
p2.load(inputFile);
String value = p2.getProperty(key);
conn = DriverManager.getConnection(url,p2);
二,ResultSet 的各个方法:
String sql2 = "select * from student order by sid";
rs = comm.executeQuery(sql2);
return rs;
rs.last();
totalsize = rs.getRow();
rs.absolute(i);


——————————linkDB.java————————————————————————
package test20100624_pages;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
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;
import java.util.Properties;

public class linkDB {

    Properties p2 = new Properties();
    ArrayList<student> arrayS = null;
    final int per_pages_size=5;
    void connect_properties() {
        FileInputStream inputFile;
        try {
            inputFile = new FileInputStream(
                    "src/test20100624_pages/p2.properties");


            try {
                p2.load(inputFile);
//                System.out.println("测试properties:"+p2);
//                测试properties:{max=1000, user=root, password=root, min=999}
//                测试properties:{max=1000, user=root, password=root, min=999}
            } catch (IOException e) {
                //
                e.printStackTrace();
            }
        } catch (FileNotFoundException e2) {
            //
            e2.printStackTrace();
        }
    }

    public String getValue(String key) {
        if (p2.containsKey(key)) {
            String value = p2.getProperty(key);// 得到某一属性的值
            return value;
        } else
            return "没有该属性";
    }

    public Connection getConnection2() {
        String url = "jdbc:mysql://localhost:3306/test_jdbc?useUnicode=true&characterEncoding=gbk";
        this.connect_properties();
        String user = this.getValue("user");
        String DbPassword = this.getValue("password");

        Connection conn = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            //conn = DriverManager.getConnection(url, user, DbPassword);
            conn = DriverManager.getConnection(url,p2);
        } 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";
            // 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();
            // System.out.println("ResultSet的总大小大小" + rs.getRow());
            totalsize = rs.getRow();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            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();
        }
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        // 测试:数据库连接
        linkDB sdl = new linkDB();
        // l.getAllStudent_info();
        // 测试:数据库表 大小
        System.out.println("ResultSet的总大小大小" + sdl.getPagesTotalSize());
        // 测试:显示 第一页和第三页的信息,还有最后一页,加上超出范围的情况。
        // 一共是18个信息,每页5个。最多是4页。
        System.out.println("第一页");
        sdl.test_per_page(1);
        System.out.println("第三页");
        sdl.test_per_page(3);
        System.out.println("最后一页");
        sdl.test_per_page(4);
        System.out.println("超出范围情况");
        sdl.test_per_page(5);
        System.out.println("异常情况");
        sdl.test_per_page(-1);
    }

}
——————mainPages.java————————————————
package test20100624_pages;

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;

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.setResizable(false);
        this.setVisible(true);

    }

    void initialization() {
        arrayS = ldb.get_per_Page(currentlyPage);
        for (Iterator i = arrayS.iterator(); i.hasNext();) {
            student s = (student) i.next();
            ta.append(s.getStudentInfo());
        }
    }

    public void next() {
        currentlyPage++;
        arrayS = ldb.get_per_Page(currentlyPage);
        for (Iterator i = arrayS.iterator(); i.hasNext();) {
            student s = (student) i.next();
            ta.append(s.getStudentInfo());
        }
    }

    public void previous() {
        currentlyPage--;
        arrayS = ldb.get_per_Page(currentlyPage);
        for (Iterator i = arrayS.iterator(); i.hasNext();) {
            student s = (student) i.next();
            ta.append(s.getStudentInfo());
        }
    }

    class MyListener implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            ta.setText("");
            if (e.getSource() == previously) {
                if (currentlyPage >= 2) {
                    currentlyPage--;
                } else {
                    ta.append("当前为第一页!\n");
                }
                arrayS = ldb.get_per_Page(currentlyPage);

                for (Iterator i = arrayS.iterator(); i.hasNext();) {
                    student s = (student) i.next();
                    ta.append(s.getStudentInfo());
                }
            } else if (e.getSource() == next) {
                ta.setText("");
                int pagesNum = ldb.getPagesNum();
                if (currentlyPage < pagesNum) {
                    currentlyPage++;
                } else {
                    ta.append("当前为最后一页!\n");
                }
                arrayS = ldb.get_per_Page(currentlyPage);

                for (Iterator i = arrayS.iterator(); i.hasNext();) {
                    student s = (student) i.next();
                    ta.append(s.getStudentInfo());
                }
            } else if (e.getSource() == first) {
                ta.setText("");
                currentlyPage = 1;
                arrayS = ldb.get_per_Page(currentlyPage);
                for (Iterator i = arrayS.iterator(); i.hasNext();) {
                    student s = (student) i.next();
                    ta.append(s.getStudentInfo());
                }
            } else if (e.getSource() == tail) {
                ta.setText("");
                currentlyPage = ldb.getPagesNum();
                arrayS = ldb.get_per_Page(currentlyPage);

                for (Iterator i = arrayS.iterator(); i.hasNext();) {
                    student s = (student) i.next();
                    ta.append(s.getStudentInfo());
                }
            }
        }
    }

    public static void main(String[] args) {
        mainPages mp = new mainPages();
    }

}

—————————student.java—————————————————————————

package test20100624_pages;

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() + "姓名" + this.getSname()
                + "密码:" + this.getSpassword());
    }

    public String getStudentInfo() {
        return "学号" + this.getSid() + "姓名" + this.getSname() + "密码:"
                + this.getSpassword()+"\n";
    }

}

————————————————————————————————
/*SQL 语句,导入就行了。
drop database test_jdbc;
create database test_jdbc;
use test_jdbc;


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');


insert into course values('1','计算机网络','40');
insert into course values('2','软件工程','40');
insert into course values('3','操作系统','40');
insert into course values('4','毛概','40');
insert into course values('5','日语','40');
insert into course values('6','编译原理','40');
insert into course values('7','程序实践4','40');
insert into course values('8','实践考核2','40');
*/

你可能感兴趣的:(java,sql,mysql,jdbc,软件测试)