mysql java 分页实体类_Java GUI+mysql+分页查询
1.要求 : 創建一個學生信息管理數據庫
2.實現分頁查詢
代碼如下:
a)學生實體類:
/***@author: Annie
* @date:2016年6月23日
* @description:學生實體類 學號+姓名+密碼*/
public classstudent {private intsid;privateString sname;privateString spassword;public student(intsid, String sname, String spassword) {this.sid =sid;this.sname =sname;this.spassword =spassword;
}public void setSid(intsid) {this.sid =sid;
}public voidsetSname(String sname) {this.sname =sname;
}public voidsetSpassword(String spassword) {this.spassword =spassword;
}public intgetSid() {return this.sid;
}publicString getSname() {return this.sname;
}publicString getSpassword() {return this.spassword;
}public voidshowStudentInfo() {
System.out.println("學號:" + this.getSid() + "\t姓名:" + this.getSname()+ "\t密碼:" + this.getSpassword());
}publicString getStudentInfo() {return "學號" + this.getSid() + "姓名" + this.getSname() + "密碼:"
+ this.getSpassword()+"\n";
}
}
b)數據庫操作類
importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.ResultSet;importjava.sql.SQLException;importjava.sql.Statement;importjava.util.ArrayList;importjava.util.Iterator;/***@author: Annie
* @date:2016年6月23日
* @description:*/
public classlinkDB {
ArrayList arrayS = null;final int per_pages_size=5;//每個頁面的長度,即每個頁面幾條數據//數據庫的連接的方法
publicConnection 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();
}returnconn;
}/*** 數據庫中查詢的代碼,返回一個結果集
**/
publicResultSet 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);returnrs;
}catch(SQLException e) {
e.printStackTrace();
}returnrs;
}/*** 獲取數據庫中的總行數
**/
public intgetPagesTotalSize() {//得到查詢的結果集
ResultSet rs = this.getAllStudent_info();int totalsize = 0;//數據總體的行數
try{
rs.last();//將光標移動到此 ResultSet 對象的最后一行//獲取當前行編號(先將光標移到最后一行,然后再獲取最后一行的下標,即可得到整個數據庫的行數)
totalsize =rs.getRow();
}catch(SQLException e) {
e.printStackTrace();
}returntotalsize;
}/*** 得到總共的頁數的方法
**/
public intgetPagesNum() {int totalsize = (Integer)this.getPagesTotalSize()/per_pages_size;return (totalsize+1);
}/*** 將從數據庫里遍歷到的數據裝到數組里
**/
public ArrayList get_per_Page(intper_pages_num) {//得到所有學生的結果集
ResultSet rs = this.getAllStudent_info();//定義一個裝學生對象的集合
ArrayList arrayS = newArrayList();int totalsize = this.getPagesTotalSize();int sid = 0;//學號
String sname = null;//姓名
String spassword = null;//密碼
if (per_pages_size * (per_pages_num - 1) 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= newstudent(sid, sname, spassword);
arrayS.add(s);
}catch(SQLException e) {
e.printStackTrace();
}
}
}else{
System.out.println("超出范圍");
}returnarrayS;
}/*** 展示數據的方法
**/
public void test_per_page(intid) {
ArrayList arrayS = this.get_per_Page(id);//遍歷數組,將數據展示在文本域里
for (Iterator i =arrayS.iterator(); i.hasNext();) {
student s=(student) i.next();
s.showStudentInfo();
}
}
}
c)主界面類
importjava.awt.BorderLayout;importjava.awt.Button;importjava.awt.Frame;importjava.awt.GridLayout;importjava.awt.Panel;importjava.awt.TextArea;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.awt.event.WindowAdapter;importjava.awt.event.WindowEvent;importjava.util.ArrayList;importjava.util.Iterator;
/***@author: Annie
* @date:2016年6月23日
* @description:*/
public class MainPages extendsFrame{private int currentlyPage = 1; //初始化當前的頁數
privatePanel center, bottom;privateButton previously, next, first, tail;privateTextArea ta;private linkDB ldb = newlinkDB();
ArrayList arrayS = null;publicMainPages() {this.previously = new Button("上一頁");this.next = new Button("下一頁");this.first = new Button("首頁");this.tail = new Button("尾頁");this.ta = newTextArea();
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= newMyListener();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(newWindowAdapter() {public voidwindowClosing(WindowEvent e) {
setVisible(false);
dispose();
System.exit(0);
}
});//對關閉窗口做監聽
this.setBounds(200, 200, 350, 180);this.setVisible(true);
}/*** 初始化第一頁
**/
voidinitialization() {
showDate();
}/*** 對按鈕做監聽的方法
**/
class MyListener implementsActionListener {public voidactionPerformed(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 voidshowDate() {
arrayS=ldb.get_per_Page(currentlyPage);
Iterator i=arrayS.iterator();while( i.hasNext()) {
student s=(student) i.next();
ta.append(s.getStudentInfo());
}
}public static voidmain(String[] args) {
MainPages mp= newMainPages();
}
}
d)創建數據庫的表格及插入數據
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');
實現效果圖:
總結
以上是生活随笔為你收集整理的mysql java 分页实体类_Java GUI+mysql+分页查询的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java io删除文件_java IO
- 下一篇: itext java_iText - P