单元 jsp留言板_第1页
单元 jsp留言板_第2页
单元 jsp留言板_第3页
单元 jsp留言板_第4页
单元 jsp留言板_第5页
已阅读5页,还剩29页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、Jsp数据库编程(例子6-9)Java辅助类:留言板(例子5-4),例子7-9:分页显示效果图,int pageSize=0; int pageAllCount=0; int showPage=0; StringBuffer presentPageResult; CachedRowSetImpl rowSet;,Get函数 set函数,Get函数,Get函数 Set函数,Get函数,字符串转换函数,showResult函数 被getPresentPageResult调用,构造函数,例子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) ,五个属性 页面尺寸:一页的记录数 总页数 当前显示的页码 当前页的所有记录 记录集?,1、构造方法中指定数据库驱动,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、将字符串转换为中文的函数,3、取得页面大小(一页显示的记录数),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、设置页面大小,连接数据库,取出book表中记录,放在rs中,将rs转换格式

6、放在rowSet对象中 取出rowSet的行数,计算得到总页数,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、取得总页数,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,反思 数组的缺点,长度固定 Int a=

10、new int5; String b=new String10; 里面只能保存一种类型 A数组中只能放整型 B数组中只能放字符串类型,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(动态数组),客服数组的两大缺点: 长度不固定,可以 随时添加 可以添加不同类型的数据,输出为:,1:引入ArrayList类,2:申明ArrayList类的对象List1,3:添加各种类型数据到List1,4:用循环取出List1各个数据打印,5:用打印语句直接打印list1对象内容,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特有的循环语法,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作业是什么?); 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类定义一个对象的数组 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对象调用方法 ,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数组生成一个新

17、的字符串 catch (Exception ee) return str; ,chString类,作用:有一个handleString函数可以将字符串由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列表循环

21、 strWatch.append(编号); strWatch.append(me.getNum(); strWatch.append(标题); strWatch.append(me.getTitle(); strWatch.append(姓名); strWatch.append(me.getAuthor(); strWatch.append(内容); strWatch.append(me.getMess(); return strWatch; ,messList类,作用:一个类,包含添加留言和取得所有留言的方法,它表示很多留言组成的列表, Insert title here 姓名 作者 内容 查看所有留言 ,提交留言界面,作用:输入留言的界面, Insert title here 返回 ,处理提交留言,作用:将留言保存到列表mess1 (一个messList类的对象)中, I

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论