《Web数据库学生实验报告2015版》由会员分享,可在线阅读,更多相关《Web数据库学生实验报告2015版(26页珍藏版)》请在人人文库网上搜索。
1、Web数据库技术教师实验指导手册及评分标准院 系: 信息科学与技术学院专 业: 信息管理与信息系统班 级: 信A1321/22任课教师: 张海 实验报告(一)院系:信息学院 课程名称:Web数据库技术 日期:班 级姓 名专 业学 号实 验 室实验名称Jdbc应用成 绩 评 定教 师 签 名实验目的1、 掌握数据库驱动的加载方式2、 掌握connection对象的使用方法3、 掌握statement对象使用方法4、 掌握事务的处理机制5、掌握数据持久层的设计实验内容connection对象、statement对象等应用1、 请设计一个工程类通过配置文件如下db.properties来获得数据库连。
2、接的相关信息,并通过该配置文件获得数据库连接对象。(20分)db.propertiesdriver=org.gjt.mm.mysql.Driverurl=jdbc:mysql:/127.0.0.1/u5B66u751Fu5E93userName=adminpwd=adminpublic class connectionFactory public static Connection getConnection() throws SQLException请把getConnection()方法补全。要求设计合理规范,必须有截图。答案:2、 已知学生定义如下:public class student。
3、 private int id;private String stuId;private String name;private String domCard;/楼栋宿舍号“31-507”private String bedNo;/床铺号public student(String stuId, String name, String domCard, String bedNo) super();this.stuId = stuId;this.name = name;this.domCard = domCard;this.bedNo = bedNo;public String toString(。
4、)return id=+id+;学号=+stuId+”;姓名=”+name+”;宿舍号=”+domCard+”;床铺号=”+bedNo;/相应get、set方法省略有一class studentDataspublic static ArrayList students=new ArrayList();staticstudent stu=new student(会计A,张三,31栋908,1);students.add(stu);stu=new student(会计A,李四,31栋908,2);students.add(stu);stu=new student(会计A,王五,31栋807,4);。
5、students.add(stu);现要求 (1)根据student类建立一个学生表用来保存student类的相关属性。(2)通过jdbc,将studentDatas的students集合中的所有学生保持到学生表中;(3)通过jdbc,将学生表中所有的宿舍是” 31栋908”学生全部调整到“20栋371”宿舍;(4)通过jdbc,删除31栋807床铺号是4的学生。20分3、 ResultSet对象操作请将上题中宿舍号最后一位是”8”的学生信息全部显示出来。4、请用perparedStatement对象保存如下:student stu=new student(信息A,刘六,31栋818,8);s。
6、tudent对象的实例stu到数据中。兴趣题:1、分页功能的实现原理方法一:JDBC实现用JDBC从ResultSet中取出一个指定范围(offset, span)的数据,可以采用这样的方法。PrepareStatement ps = con.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); ps.setMaxRows(offset span);/最终读到第几条rs = ps.executeQuery();rs.absolute(offset);/定位到每页的开始记录处wh。
7、ile(rs.next()/ conn.prepareStatement(sql,游标类型,能否更新记录);/ 游标类型:/ ResultSet.TYPE_FORWORD_ONLY:只进游标/ ResultSet.TYPE_SCROLL_INSENSITIVE:可滚动。但是不受其他用户对数据库更改的影响。/ ResultSet.TYPE_SCROLL_SENSITIVE:可滚动。当其他用户更改数据库时这个记录也会改变。/ 能否更新记录:/ ResultSet.CONCUR_READ_ONLY,只读/ ResultSet.CONCUR_UPDATABLE,可更新方法二:数据库的sql语言实现My。
8、sql 的limit SQL为Select * from message where forum_id = ? and created_time ? order by created_time desc limit ?, ?后面的两个limit ?, ? 分别为 offset, span。Oracle的limit SQL为select * from ( select row_.*, rownum rownum_ from (Select * from message where forum_id = ? and created_time ? order by created_time desc。
9、) row_ where rownum ?后面的两个limit ?, ? 分别为 offset span, offset。2、请执行在mysql中执行insert into 宿舍表(学号,姓名,宿舍,床铺) values (会计A,张三,24栋908,1)两次。请通过JDBC将宿舍表中学号重复的记录去掉,只保留其中的一条。5、事务处理请先在mysql中运行如下语句:CREATE TABLE 账户表 (账户名 varchar(10) NOT NULL DEFAULT ,金额 double DEFAULT NULL,PRIMARY KEY (账户名) ENGINE=InnoDB DEFAULT C。
10、HARSET=utf8;- - Records of 账户表- -INSERT INTO 账户表 VALUES (张三, 100);INSERT INTO 账户表 VALUES (李四, 200);INSERT INTO 账户表 VALUES (王五, 50);请运行如下程序查看结果(1)程序1:public class trans1 /* param args*/public static void main(String args) Connection conn1=null;Statement stmn1=null;try conn1=connectionFactory.getConne。
11、ction();conn1.setAutoCommit(true);stmn1=conn1.createStatement();stmn1.executeUpdate(update 账户表 set 金额=金额+5 where 账户名=张三);System.out.println(update语句执行); catch (SQLException e) / TODO Auto-generated catch blocke.printStackTrace();finallyif(stmn1!=null)try stmn1.close(); catch (SQLException e) / TODO 。
12、Auto-generated catch blocke.printStackTrace();if(conn1!=null)try conn1.close(); catch (SQLException e) / TODO Auto-generated catch blocke.printStackTrace();(2)程序2:public class trans2 /* param args*/public static void main(String args) Connection conn1=null;Statement stmn1=null;try conn1=connectionFa。
13、ctory.getConnection();conn1.setAutoCommit(false);stmn1=conn1.createStatement();stmn1.executeUpdate(update 账户表 set 金额=金额+5 where 账户名=张三);System.out.println(update语句执行); catch (SQLException e) / TODO Auto-generated catch blocke.printStackTrace();finallyif(stmn1!=null)try stmn1.close(); catch (SQLExcep。
14、tion e) / TODO Auto-generated catch blocke.printStackTrace();if(conn1!=null)try conn1.close(); catch (SQLException e) / TODO Auto-generated catch blocke.printStackTrace();请查询数据库记录内容。(3)完整事务程序3:public class trans2 /* param args*/public static void main(String args) Connection conn1=null;Statement stm。
15、n1=null;try conn1=connectionFactory.getConnection();conn1.setAutoCommit(false);stmn1=conn1.createStatement();stmn1.executeUpdate(update 账户表 set 金额=金额+5 where 账户名=张三);conn1.commit();System.out.println(insert语句执行); catch (SQLException e) / TODO Auto-generated catch blocke.printStackTrace();try conn1.r。
16、ollback(); catch (SQLException e1) / TODO Auto-generated catch blocke1.printStackTrace();finallyif(stmn1!=null)try stmn1.close(); catch (SQLException e) / TODO Auto-generated catch blocke.printStackTrace();if(conn1!=null)try conn1.close(); catch (SQLException e) / TODO Auto-generated catch blocke.pr。
17、intStackTrace();如果需要将下面update及insert作为一个事务来整体提交,请问下面代码如果改?public class trans3 /* param args*/public static void main(String args) Connection conn1=null;Connection conn2=null;Statement stmn1=null;Statement stmn2=null;try conn1=connectionFactory.getConnection();conn2=connectionFactory.getConnection();。
18、stmn1=conn1.createStatement();stmn1.executeUpdate(update 账户表 set 金额=金额+5 where 账户名=张三);System.out.println(update语句执行);stmn2=conn2.createStatement();stmn2.executeUpdate(INSERT INTO 账户表 VALUES (张三, 500);System.out.println(insert语句执行); catch (SQLException e) / TODO Auto-generated catch blocke.printStac。
19、kTrace();finallyif(stmn1!=null)try stmn1.close(); catch (SQLException e) / TODO Auto-generated catch blocke.printStackTrace();if(stmn2!=null)try stmn1.close(); catch (SQLException e) / TODO Auto-generated catch blocke.printStackTrace();if(conn1!=null)try conn1.close(); catch (SQLException e) / TODO 。
20、Auto-generated catch blocke.printStackTrace();已知有线程类定义如下:import java.sql.Connection;import java.sql.SQLException;import java.sql.Statement;import java.util.Date;public class clientThread extends Threadprivate String sql=null;private int wait=4000;public String getSql() return sql;public void setSql(。
21、String sql) this.sql = sql;public int getWait() return wait;public void setWait(int wait) this.wait = wait;public void run()Connection conn= connectionFactory.getConnection();try conn.setAutoCommit(false);Statement stmn=conn.createStatement();stmn.executeUpdate(sql);System.out.println(new Date().toS。
22、tring()+ 执行 +sql);this.sleep(wait);conn.commit();System.out.println(线程结束+(new Date().toString(); catch (Exception e) / TODO Auto-generated catch blocke.printStackTrace();有两个测试类client1.javapublic class client1 /* param args*/public static void main(String args) clientThread client=new clientThread();。
23、client.setWait(10000);client.setSql(update 宿舍表 set 姓名=姓名+1 where 床铺=1);client.start();public class client2 /* param args*/public static void main(String args) clientThread client=new clientThread();client.setWait(6000);client.setSql(update 宿舍表 set 姓名=姓名+A where 床铺=1);client.start();请先执行client1然后马上执行。
24、client2,等所有程序,数据库中床铺=1的记录内容是什么?为什么会这样?实验报告(二)院系:信息学院 课程名称:Web数据库技术 日期:班 级A1321姓 名黄伟峰专 业信息管理与信息系统学 号实 验 室实验名称Servlet应用成 绩 评 定教 师 签 名实验目的1、 掌握servlet的配置方法2、 掌握web服务器的配置过程3、 掌握HttpServlet类的用法4、 掌握session等对象的用法5、 掌握请求转发功能的应用6、掌握SerlvetConfig上下文的用法实验内容Servlet配置、servlet设计、会话管理、请求流转、上下文应用1、 请建立一个servlet类用于。
25、显示当前的系统时间。写出它的url配置写法20分。答案:public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException response.setContentType(text/html);PrintWriter out = response.getWriter();out.println();out.println();out.println( A Servlet);out.println( );out.println( );Dat。
26、e date = new Date() ;out.print(date.toLocaleString() ;out.println( );out.println( );out.println();out.flush();out.close();url配置写法:生成servlt时将工程名去掉,只保留文件名,便于访问2、领会多用户并发访问的机制,请建立一个HttpServlet“servletUsers”类,在该类的成员代码如下:public class serveltUsers extends HttpServlet private Integer x=1;public void doGet(H。
27、ttpServletRequest request, HttpServletResponse response)throws ServletException, IOException this.doPost(request, response);public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException response.setContentType(text/html);response.setCharacterEncodin。
28、g(utf-8);PrintWriter out = response.getWriter();out.println();out.println();out.println( A Servlet);out.println( );out.println(当前x的值是+x);out.flush();synchronized (x) x+;Thread thread=Thread.currentThread();try thread.sleep(1000*5); catch (InterruptedException e) / TODO Auto-generated catch blocke.pr。
29、intStackTrace();out.println(当前serlvet实例阻塞5秒后,当前x的值是+x);out.println( );out.println();out.flush();out.close();请在5秒内用两个浏览器窗口浏览该HttpServlet类,看看效果如何。为什么是这个效果!20分。答:效果是当第一个界面还未刷新,第二个界面无法看到效果。原因是:在全局变量x上加了锁,保证了同一时间,只有一个浏览器才能获得x当前值3、 请设计一个servlet,当连续三次访问该servlet是分别向客户输出:第1次: 用户浏览器中显示:蓝色颜色的 “联系访问中的第1次”第2次: 用。
30、户浏览器中显示:红色颜色的 “联系访问中的第2次”第3次: 用户浏览器中显示:黑色颜色的 “联系访问中的第3次”public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException this.doPost(request, response) ;public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException,。
31、 IOException request.setCharacterEncoding(utf-8) ;response.setCharacterEncoding(utf-8) ;response.setContentType(text/html);PrintWriter out = response.getWriter();out.println();out.println();out.println( A Servlet);out.println( );out.print( );x+ ;/蓝色 0,0,255 红色:rgb(255,0,0) 黑色:rgb(0,0,0)if (x=1) out.。
32、print();else if (x=2) out.print();else if (x=3) out.print();x = 0 ;out.print(第+x+次:);out.print(“连续访问中的第+x+次”);out.print( );out.print( );out.print( );out.println();out.flush();out.close();4、 请将提供的servelt.rar工程解压后添加到myeclipse工作区中,打开mysql将工程中的db目录下的”学生库.sql”导入到mysql数据库中。在工程的serviceDao包中有一个studentServic。
33、esDao,请根据代码中注释的功能结合jdbc中dao模式的设计要求将public student login(String stuId,String name)throws myExceptionstudent stu = new student() ;if(stuId!=null&!.equals(stuId)&name!=null&!.equals(name)return null ;else Connection conn=null;try conn=connectionFactory.getConnection();String sql=select * from 学生基本情况表 w。
34、here 学号 =+stuId+ and 姓名=+name+;Statement stat=conn.createStatement();ResultSet rs=stat.executeQuery(sql);stu.setName(name) ;conn.commit();connectionFactory.closeAll(rs, stat, conn); catch (SQLException e) / TODO Auto-generated catch blocke.printStackTrace();return stu ; 补充完整。20分。5、 现在在webroot下有index。
35、.jsp用来展现学生的登录,现在要求学生输入学号、姓名后交给login这个servlet去处理,login的功能要求如下:(1) 判断用户输入的学号和姓名在学生基本表中是否存在,如果存在提前该学生信息,并将这个学生信息交给另外一个servlet-“dispStudent”去显示。(2) 如果用户输入的学号和姓名在学生基本表中不存在,就直接给用户反馈”找不到输入姓名的学生”。DispStudent这个servlet的功能:显示请求转发过来的学生所有信息。1)public class login extends HttpServlet public void doGet(HttpServletRe。
36、quest request, HttpServletResponse response)throws ServletException, IOException doPost(request, response) ;public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException request.setCharacterEncoding(utf-8) ;response.setCharacterEncoding(utf-8) ;resp。
37、onse.setContentType(text/html;charset=utf-8);PrintWriter out = response.getWriter();boolean isOk = false;String name=request.getParameter(name);String stuId=request.getParameter(stuId);if(name=null|.equals(name)|stuId=null|.equals(stuId)out.print(用户名或者密码为空) ;elseConnection conn = null ;try conn = co。
38、nnectionFactory.getConnection() ;String sql = select * from 学生基本情况表 where 姓名 =+name+ and 学号=+stuId+ ;Statement stat = conn.createStatement() ;ResultSet rs = stat.executeQuery(sql) ;if (rs.next() isOk = true ; conn.commit() ;connectionFactory.closeAll(rs, stat, conn ,null) ; catch (SQLException e) / 。
39、TODO Auto-generated catch blocke.printStackTrace();if(isOk=true)request.setAttribute(stuname,name);request.getRequestDispatcher(/DispStudent).forward(request, response); elseout.print(用户名或者密码错误) ; / response.sendRedirect(index.jsp); 2)public class DispStudent extends HttpServlet public void doGet(Ht。
40、tpServletRequest request, HttpServletResponse response)throws ServletException, IOException this.doPost(request, response);public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException response.setContentType(text/html;charset=utf-8);/String classNa。
41、me=request.getParameter(stuId).substring(0,5);String stuname=(String)request.getAttribute(stuname);student stu=null;Connection conn=null;try conn=connectionFactory.getConnection();String sql=select * from 学生基本情况表 where 姓名 like +stuname+;Statement stat=conn.createStatement();ResultSet rs=stat.execute。
42、Query(sql);System.out.println(rs.toString();while(rs.next()stu=new student();stu.setId(rs.getInt(id);stu.setStuId(rs.getString(学号);stu.setPro(rs.getString(专业名称);stu.setName(rs.getString(姓名);stu.setSex(rs.getString(性别);conn.commit();connectionFactory.closeAll(rs, null, conn, null); catch (SQLExceptio。
43、n e) / TODO Auto-generated catch blocke.printStackTrace();PrintWriter out = response.getWriter();out.println();out.println();out.println( A Servlet);out.println( id学号姓名专业名称性别);out.print(+stu.getId()+stu.getStuId()+stu.getName()+stu.getPro()+stu.getSex()+);out.println( );out.println();out.flush();out。
44、.close();6、 用户访问统计,当用户首次访问某个servlet时,该servlet的访问总次数加1,如果该用户已经访问了该servlet,再访问时不加1,要求该统计次数能在其他servlet中被调用。/统计访问次数public int cisu(HttpServletRequest request, HttpServletResponse response,String name) throws Exception /判断是否是同一个用户Cookie cookies = request.getCookies() ;for (int i = 0; cookies!=null&i cook。
45、ies.length; i+) if (cookiesi.getName().equals(name) if (!URLDecoder.decode(cookiesi.getValue(), UTF-8).equals(name) x+ ;/用户再次访问Cookie cookie = new Cookie(name,URLEncoder.encode(name, UTF-8) ;cookie.setMaxAge(24*3600) ;cookie.setPath(/servelt) ;response.addCookie(cookie) ;return x ;7、 cookie使用,当用户输入用。
46、户名是张三,密码是1234时,请在servlet中创建一个有效时间是300秒的cookie对象,并回传到用户浏览器,浏览器再访问另外一个servlet,查看该cookie是否存在。保存用户名和密码if (name.equals(张三)& stuId.equals(1234) String name1 = URLEncoder.encode(name, UTF-8);Cookie cookie = new Cookie(name, name1) ;cookie.setMaxAge(300) ; cookie.setPath(/servelt) ;response.addCookie(cookie。
47、) ;String stuId1 = URLEncoder.encode(stuId, UTF-8);Cookie cookie2 = new Cookie(stuId, stuId1) ;cookie2.setMaxAge(300) ;cookie2.setPath(/servelt) ;response.addCookie(cookie2) ;获取用户名和密码public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException requ。
48、est.setCharacterEncoding(utf-8);response.setCharacterEncoding(utf-8);response.setContentType(text/html);PrintWriter out = response.getWriter() ;Cookie cookie = request.getCookies() ;for (int i = 0;cookie!=null& i cookie.length; i+) if (cookiei.getName().equals(name) String name = URLDecoder.decode(c。
49、ookiei.getValue(), UTF-8);out.println(用户名:+name) ;if (cookiei.getName().equals(stuId) String stuId = URLDecoder.decode(cookiei.getValue(), UTF-8);out.println(密码:+stuId) ;8、请设计一种过滤器实现权限控制机制。如果用户进入web应用没有登入时,要求用户必须进入登入页面。20分public class loginFiler implements Filter public void init(FilterConfig filter。
50、Config) throws ServletException / TODO Auto-generated method stubpublic void doFilter(ServletRequest request, ServletResponse response,FilterChain chain) throws IOException, ServletException / 获得在下面代码中要用的request,response,session对象HttpServletRequest servletRequest = (HttpServletRequest) request;HttpServletResponse servletResponse = (HttpServletResponse) response。