12单元 jsp留言板

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

Jsp数据库编程(例子6-9)Java辅助类:留言板(例子5-4)例子7-9:分页显示效果图intpageSize=0;intpageAllCount=0;intshowPage=0;StringBufferpresentPageResult;CachedRowSetImplrowSet;Get函数set函数Get函数Get函数Set函数Get函数字符串转换函数showResult函数被getPresentPageResult调用构造函数例子7-9javabeanpackagech7.ch7_9;importjava.sql.*;importcom.sun.rowset.*;publicclassshowbypageBean{intpageSize=0;intpageAllCount=0;intshowPage=0;StringBufferpresentPageResult;CachedRowSetImplrowSet;publicshowbypageBean(){presentPageResult=newStringBuffer();try{Class.forName(com.microsoft.sqlserver.jdbc.SQLServerDriver);}catch(Exceptione){}}五个属性页面尺寸:一页的记录数总页数当前显示的页码当前页的所有记录记录集?1、构造方法中指定数据库驱动publicStringgetString(Stringstr){Strings=str.trim();try{bytebb[]=s.getBytes();s=newString(bb);}catch(Exceptione){}returns;}publicintgetPageSize(){returnpageSize;}2、将字符串转换为中文的函数3、取得页面大小(一页显示的记录数)publicvoidsetPageSize(intsize){pageSize=size;Stringurl=jdbc:sqlserver://localhost:1433;databaseName=javaTeach;try{System.out.println(url);Stringuserid=sa;Stringuserpwd=123;Connectionconn=DriverManager.getConnection(url,userid,userpwd);Statementstmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);ResultSetrs=stmt.executeQuery(select*frombook);rowSet=newCachedRowSetImpl();rowSet.populate(rs);stmt.close();conn.close();rowSet.last();intm=rowSet.getRow();intn=pageSize;pageAllCount=(m%n==0)?(m/n):(m/n+1);}catch(Exceptione){}}4、设置页面大小连接数据库,取出book表中记录,放在rs中将rs转换格式放在rowSet对象中取出rowSet的行数,计算得到总页数publicintgetPageAllCount(){returnpageAllCount;}publicintgetShowPage(){returnshowPage;}publicvoidsetShowPage(intshowPage){this.showPage=showPage;}publicStringBuffergetPresentPageResult(){if(showPagepageAllCount){showPage=pageAllCount;}if(showPage=0){showPage=1;}presentPageResult=showRecord(showPage);returnpresentPageResult;}6、取得当前显示的页码7、设置当前显示的页码5、取得总页数8、取得当前页的所有记录教材中是反过来的,请改正publicStringBuffershowRecord(intpage){StringBuffershowMessage=newStringBuffer();showMessage.append(tableborder=1);showMessage.append(tr);showMessage.append(thwidth=50align=center书号/th);showMessage.append(thwidth=50align=center书名/th);showMessage.append(thwidth=50align=center作者/th);showMessage.append(thwidth=50align=center出版社/th);showMessage.append(thwidth=50align=center单价/th);showMessage.append(/tr);try{rowSet.absolute((page-1)*pageSize+1);for(inti=1;i=pageSize;i++){showMessage.append(tr);showMessage.append(thwidth=50align=center+rowSet.getString(1)+/th);showMessage.append(thwidth=50align=center+rowSet.getString(2)+/th);showMessage.append(thwidth=50align=center+rowSet.getString(3)+/th);showMessage.append(thwidth=50align=center+rowSet.getString(4)+/th);showMessage.append(thwidth=50align=center+rowSet.getString(5)+/th);showMessage.append(/tr);rowSet.next();}}catch(SQLExceptionexp){}showMessage.append(/table);returnshowMessage;}}9、显示某页的页码例子7-9%@pagelanguage=javacontentType=text/html;charset=GBKpageEncoding=GBK%htmlhead%@pageimport=ch7.ch7_9.showbypageBean;%titleInserttitlehere/title/headbodyjsp:useBeanid=query1class=ch7.ch7_9.showbypageBeanscope=session/jsp:useBeanjsp:setPropertyname=query1property=pageSizevalue=2/共有jsp:getPropertyname=query1property=pageAllCount/页br每页最多显示jsp:getPropertyname=query1property=pageSize/条记录brjsp:setPropertyname=query1property=showPage/!--显示表中记录--jsp:getPropertyname=query1property=presentPageResult/br当前显示第jsp:getPropertyname=query1property=showPage/页br重要单击“上一页”或“下一页”按钮查看记录tabletrtdformaction=inputtype=hiddenname=showPagevalue=%=query1.getShowPage()-1%inputtype=submitname=gvalue=前一页/form/tdtdformaction=inputtype=hiddenname=showPagevalue=%=query1.getShowPage()+1%inputtype=submitname=gvalue=后一页/form/tdtdformaction=inputtype=textname=showPagevalue=1size=5inputtype=submitname=gvalue=提交/form/td/tr/table/body/html重要•留言板例子5-4反思数组的缺点长度固定Inta[]=newint[5];Stringb[]=newString[10];里面只能保存一种类型A数组中只能放整型B数组中只能放字符串类型importjava.util.ArrayList;publicclassdemo1{publicstaticvoidmain(String[]args){ArrayListlist1=newArrayList();list1.add(newString(aa));list1.add(9);list1.add('c');list1.add(newInteger(900));for(inti=0;ilist1.size();i++){System.out.println(list1.get(i));}System.out.println(list1);}}新的知识:泛型ArrayList(动态数组)客服数组的两大缺点:长度不固定,可以随时添加可以添加不同类型的数据输出为:1:引入ArrayList类2:申明ArrayList类的对象List13:添加各种类型数据到List14:用循环取出List1各个数据打印5:用打印语句直接打印list1对象内容aa9c900[aa,9,c,900]1固定对象的泛型类importjava.util.ArrayList;publicclassdemo2{publicstaticvoidmain(String[]args){ArrayListStringlist1=newArrayListString();list1.add(newString(aa));list1.add(ddooo);list1.add(----);list1.add(newString(34));for(Stringme:list1){System.out.println(me);}}}输出为ArrayList特有的循环语法2aaddooo----34例子5-4的原型publicclassmessage{Stringtitle;Stringauthor;Stringmess;publicStringgetTitle(){returntitle;}publicvoidsetTitle(Stringtitle){this.title=title;}publicStringgetAuthor(){returnauthor;}publicvoidsetAuthor(Stringauthor){this.author=author;}publicStringgetMess(){returnmess;}publicvoidsetMess(Stringmess){this.mess=mess;}publicmessage(Stringti

1 / 33
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功