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

下载本文档

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

文档简介

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 9javabean packagech7 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 showPage pageAllCount showPage pageAllCount if showPage 0 showPage 1 presentPageResult showRecord showPage returnpresentPageResult 6 取得当前显示的页码 7 设置当前显示的页码 5 取得总页数 8 取得当前页的所有记录 教材中是反过来的 请改正 publicStringBuffershowRecord intpage StringBuffershowMessage newStringBuffer showMessage append showMessage append showMessage append 书号 showMessage append 书名 showMessage append 作者 showMessage append 出版社 showMessage append 单价 showMessage append try rowSet absolute page 1 pageSize 1 for inti 1 i showMessage append rowSet getString 1 showMessage append rowSet getString 2 showMessage append rowSet getString 3 showMessage append rowSet getString 4 showMessage append rowSet getString 5 showMessage append rowSet next catch SQLExceptionexp showMessage append returnshowMessage 9 显示某页的页码 例子7 9 Inserttitlehere共有页每页最多显示条记录当前显示第页 重要 单击 上一页 或 下一页 按钮查看记录 重要 留言板例子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 i list1 size i System out println list1 get i System out println list1 新的知识 泛型ArrayList 动态数组 客服数组的两大缺点 长度不固定 可以随时添加可以添加不同类型的数据 输出为 1 引入ArrayList类 2 申明ArrayList类的对象List1 3 添加各种类型数据到List1 4 用循环取出List1各个数据打印 5 用打印语句直接打印list1对象内容 1 固定对象的泛型类 importjava util ArrayList publicclassdemo2 publicstaticvoidmain String args ArrayListlist1 newArrayList list1 add newString aa list1 add ddooo list1 add list1 add newString 34 for Stringme list1 System out println me 输出为 ArrayList特有的循环语法 2 aaddooo 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 Stringtitle Stringauthor Stringmess this title title this author author this mess mess importjava util ArrayList publicclassdemo3 publicstaticvoidmain String args ArrayListlist1 newArrayList list1 add newmessage 天气 a 今天下雨了吗 list1 add newmessage 功课 b 今天java作业是什么 for messageme list1 System out println me getTitle System out println me getAuthor System out println me getMess 3 运行例子5 4看效果请大家自己分析例子5 4的代码 新的知识 泛型类ArrayList 使用Student类定义一个对象Studenta newStudent 使用message类定义一个对象的数组ArrayListmessList newArrayList 定义和使用泛型类ArrayList 1 ArrayListmessList newArrayList 2 messagemtemp newmessage messList add mtemp 3 for messageme messList 对me对象调用方法 packagech5 eg5 4 importjava util ArrayList classchString publicStringhandleString Strings Stringstr s try byteb str getBytes ISO 8859 1 str newString b 用b数组生成一个新的字符串 catch Exceptionee returnstr chString类 作用 有一个handleString函数可以将字符串由ISO 8859 1编码转为正常的中文显示 classmessage Stringtitle null Stringauthor null Stringmess null longnum chStringchstr newchString publicStringgetTitle returntitle publicvoidsetTitle Stringtitle this title chstr handleString title publicStringgetAuthor returnauthor publicvoidsetAuthor Stringauthor this author chstr handleString author publicStringgetMess returnmess publicvoidsetMess Stringmess this mess chstr handleString mess publiclonggetNum returnnum publicvoidsetNum longnum this num num 四个属性的定义及初始值 四个属性的get和set方法 message类 作用 表示一条留言 有标题作者留言和编号 publicclassmessList ArrayListmessL null longnumber publicmessList messL newArrayList number 0 synchronizedpublicvoidadd StringaAuthor StringaTitle StringaMess if equals aAuthor equals aTitle equals aMess number messagemtemp newmessage mtemp setAuthor aAuthor mtemp setTitle aTitle mtemp setMess aMess mtemp setNum number messL add mtemp 将mtemp对象加入到messL列表中 publicStringBuffergetMessSet StringBufferstrWatch newStringBuffer for messageme messL 将messL列表循环strWatch append 编号 strWatch append me getNum strWatch append 标题 strWatch append me getTitle strWatch append 姓名 strWatch append me getAuthor strWatch append 内容 strWatch append me getMess returnstrWatch messList类 作用 一个类 包含添加留言和取得所有留言的方法 它表示很多留言组成的列表 Inserttitlehere姓名作者内容查看所有留言 提交留言界面 作用 输入留言的界面 Inserttitlehere返回 处理提交留言 作用 将留言保存到列表mess1 一个messList类的对象 中 Inserttitlehere 如果有的

温馨提示

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

评论

0/150

提交评论