單元 jsp留言板_第1頁
單元 jsp留言板_第2頁
單元 jsp留言板_第3頁
單元 jsp留言板_第4頁
單元 jsp留言板_第5頁
已閱讀5頁,還剩29頁未讀 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領

文檔簡介

1、Jsp數(shù)據(jù)庫編程(例子6-9)Java輔助類:留言板(例子5-4),例子7-9:分頁顯示效果圖,int pageSize=0; int pageAllCount=0; int showPage=0; StringBuffer presentPageResult; CachedRowSetImpl rowSet;,Get函數(shù) set函數(shù),Get函數(shù),Get函數(shù) Set函數(shù),Get函數(shù),字符串轉換函數(shù),showResult函數(shù) 被getPresentPageResult調(diào)用,構造函數(shù),例子7-9 javabean,package ch7.ch7_9; import java.sql.*; impo

2、rt com.sun.rowset.*; public class showbypageBean int pageSize=0; int pageAllCount=0; int showPage=0; StringBuffer presentPageResult; CachedRowSetImpl rowSet; public showbypageBean() presentPageResult=new StringBuffer(); try Class.forName(com.microsoft.sqlserver.jdbc.SQLServerDriver); catch (Exceptio

3、n e) ,五個屬性 頁面尺寸:一頁的記錄數(shù) 總頁數(shù) 當前顯示的頁碼 當前頁的所有記錄 記錄集?,1、構造方法中指定數(shù)據(jù)庫驅動,public String getString(String str) String s=str.trim(); try byte bb=s.getBytes(); s=new String(bb); catch (Exception e) return s; public int getPageSize() return pageSize; ,2、將字符串轉換為中文的函數(shù),3、取得頁面大?。ㄒ豁擄@示的記錄數(shù)),public void setPageSize(int

4、size) pageSize=size; String url=jdbc:sqlserver:/localhost:1433;databaseName=javaTeach; try System.out.println(url); String userid=sa; String userpwd=123; Connection conn=DriverManager.getConnection(url,userid,userpwd); Statement stmt=conn.createStatement( ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.C

5、ONCUR_READ_ONLY); ResultSet rs=stmt.executeQuery(select * from book); rowSet=new CachedRowSetImpl(); rowSet.populate(rs); stmt.close(); conn.close(); rowSet.last(); int m=rowSet.getRow(); int n=pageSize; pageAllCount=(m%n=0)?(m/n):(m/n+1); catch (Exception e) ,4、設置頁面大小,連接數(shù)據(jù)庫,取出book表中記錄,放在rs中,將rs轉換格式

6、放在rowSet對象中 取出rowSet的行數(shù),計算得到總頁數(shù),public int getPageAllCount() return pageAllCount; public int getShowPage() return showPage; public void setShowPage(int showPage) this.showPage = showPage; public StringBuffer getPresentPageResult() if (showPagepageAllCount) showPage=pageAllCount; if (showPage=0) show

7、Page=1; presentPageResult=showRecord(showPage); return presentPageResult; ,6、取得當前顯示的頁碼,7、設置當前顯示的頁碼,5、取得總頁數(shù),8、取得當前頁的所有記錄,教材中是反過來的,請改正,public StringBuffer showRecord (int page) StringBuffer showMessage=new StringBuffer(); showMessage.append(); showMessage.append(); showMessage.append(書號); showMessage.

8、append(書名); showMessage.append(作者); showMessage.append(出版社); showMessage.append(單價); showMessage.append(); try rowSet.absolute(page-1)*pageSize+1); for (int i=1;i); showMessage.append(+rowSet.getString(1)+); showMessage.append(+rowSet.getString(2)+); showMessage.append(+rowSet.getString(3)+); showMe

9、ssage.append(+rowSet.getString(4)+); showMessage.append(+rowSet.getString(5)+); showMessage.append(); rowSet.next(); catch (SQLException exp) showMessage.append(); return showMessage; ,9、顯示某頁的頁碼,例子7-9, Insert title here 共有頁 每頁最多顯示 條記錄 當前顯示第 頁,重要,單擊“上一頁”或“下一頁”按鈕查看記錄 ,重要,留言板例子 5-4,反思 數(shù)組的缺點,長度固定 Int a=

10、new int5; String b=new String10; 里面只能保存一種類型 A數(shù)組中只能放整型 B數(shù)組中只能放字符串類型,import java.util.ArrayList; public class demo1 public static void main(String args) ArrayList list1 = new ArrayList(); list1.add( new String(aa); list1.add( 9); list1.add( c); list1.add( new Integer(900); for(int i=0;ilist1.size();i+

11、) System.out.println(list1.get(i); System.out.println(list1); ,新的知識:泛型ArrayList(動態(tài)數(shù)組),客服數(shù)組的兩大缺點: 長度不固定,可以 隨時添加 可以添加不同類型的數(shù)據(jù),輸出為:,1:引入ArrayList類,2:申明ArrayList類的對象List1,3:添加各種類型數(shù)據(jù)到List1,4:用循環(huán)取出List1各個數(shù)據(jù)打印,5:用打印語句直接打印list1對象內(nèi)容,1,固定對象的泛型類,import java.util.ArrayList; public class demo2 public static void

12、main(String args) ArrayList list1 = new ArrayList(); list1.add( new String(aa); list1.add( ddooo); list1.add(-); list1.add( new String(34); for (String me:list1) System.out.println(me); ,輸出為,ArrayList特有的循環(huán)語法,2,aa ddooo - 34,例子5-4的原型,public class message String title; String author; String mess; publ

13、ic String getTitle() return title; public void setTitle(String title) this.title = title; public String getAuthor() return author; public void setAuthor(String author) this.author = author; public String getMess() return mess; public void setMess(String mess) this.mess = mess; public message(String

14、title, String author, String mess) this.title = title; this.author = author; this.mess = mess; ,import java.util.ArrayList; public class demo3 public static void main(String args) ArrayList list1=new ArrayList (); list1.add(new message(天氣?,a,今天下雨了嗎?); list1.add(new message(功課?,b,今天java作業(yè)是什么?); for (

15、message me :list1) System.out.println(me.getTitle(); System.out.println(me.getAuthor(); System.out.println(me.getMess(); ,3,運行例子5-4看效果 請大家自己分析例子5-4的代碼,新的知識:泛型類ArrayList,使用Student類定義一個對象 Student a=new Student(); 使用message類定義一個對象的數(shù)組 ArrayList messList= new ArrayList();,定義和使用泛型類ArrayList,1、 Array List

16、messList= new ArrayList(); 2、 message mtemp=new message(); messList.add(mtemp); 3、 for (message me:messList) / 對me對象調(diào)用方法 ,package ch5.eg5_4; import java.util.ArrayList; class chString public String handleString(String s) String str=s; try byte b=str.getBytes(ISO-8859-1); str=new String(b);/用b數(shù)組生成一個新

17、的字符串 catch (Exception ee) return str; ,chString類,作用:有一個handleString函數(shù)可以將字符串由ISO-8859-1編碼轉為正常的中文顯示,class message String title=null;String author=null; String mess=null;long num; chString chstr=new chString(); public String getTitle() return title; public void setTitle(String title) this.title = chstr

18、.handleString(title); public String getAuthor() return author; public void setAuthor(String author) this.author = chstr.handleString(author); public String getMess() return mess; public void setMess(String mess) this.mess = chstr.handleString(mess); public long getNum() return num; public void setNu

19、m(long num) this.num = num; ,四個屬性的定義及初始值,四個屬性的get和set方法,message類,作用:表示一條留言,有標題 作者 留言和編號,public class messList ArrayList messL=null; long number; public messList() messL=new ArrayList(); number=0; synchronized public void add(String aAuthor,String aTitle,String aMess) if (!(.equals(aAuthor)|(.equals(

20、aTitle)|(.equals(aMess) number+; message mtemp=new message(); mtemp.setAuthor(aAuthor);mtemp.setTitle(aTitle); mtemp.setMess(aMess);mtemp.setNum(number); messL.add(mtemp);/將mtemp對象加入到messL列表中 public StringBuffer getMessSet() StringBuffer strWatch=new StringBuffer(); for (message me:messL)/將messL列表循環(huán)

21、 strWatch.append(編號); strWatch.append(me.getNum(); strWatch.append(標題); strWatch.append(me.getTitle(); strWatch.append(姓名); strWatch.append(me.getAuthor(); strWatch.append(內(nèi)容); strWatch.append(me.getMess(); return strWatch; ,messList類,作用:一個類,包含添加留言和取得所有留言的方法,它表示很多留言組成的列表, Insert title here 姓名 作者 內(nèi)容 查看所有留言 ,提交留言界面,作用:輸入留言的界面, Insert title here 返回 ,處理提交留言,作用:將留言保存到列表mess1 (一個messList類的對象)中, I

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論