Web编程技术补考模拟试卷(B卷) 下载本文

内容发布更新时间 : 2024/11/9 10:49:43星期一 下面是文章的全部内容请认真阅读。

%>

<%=fTitle.getId()%>

<%=fTitle.getTitleName()%> <%=fTitle.getCreator()%> <%=fTitle.getCreateTime()%> <%} } %>

NewsFirstTitleDB6.java package jmu; import java.sql.*; import java.util.*;

import jmu.FirstLevelTitle;

public class NewsFirstTitleDB6 {

/**从数据库中取出所有新闻一级标题,并封装在集合中返回 */ public List getAllFirstLevelTitleList() { List list = new ArrayList(); Connection dbConnection = null; PreparedStatement pStatement = null; ResultSet res = null; try { dbConnection = ConnectionManager.getConnection(); String strSql = \ pStatement = dbConnection.prepareStatement(strSql); res = pStatement.executeQuery(); while (res.next()) { int id = res.getInt(\ String title = res.getString(2); String creator = res.getString(\ //Date time = res.getDate(\ java.util.Date time = res.getDate(4); FirstLevelTitle fTitle = new FirstLevelTitle(id, title, creator, time); list.add(fTitle); } } catch (SQLException sqlE) { sqlE.printStackTrace(); } finally { ConnectionManager.closeResultSet(res); ConnectionManager.closeStatement(pStatement); ConnectionManager.closeConnection(dbConnection); }

return list; } }

addFirstTitle.jsp

保存页面 saveFirstTitle.jsp <% //指定请求的编码为GBK request.setCharacterEncoding(\ //获取用户输入 String titleName = request.getParameter(\ FirstLevelTitle fTitle = new FirstLevelTitle(); fTitle.setTitleName(titleName); fTitle.setCreator(\管理员\ //向数据库中插入一行记录 FirstLevelTitleDbOpreation dbOpreation = new FirstLevelTitleDbOpreation(); int result = dbOpreation.insertOneRecord(fTitle); //如果插入成功,跳转至成功页面;否则跳转至失败页面 if (result > 0) { request.getRequestDispatcher(\ } else { request.getRequestDispatcher(\ } %>

FirstLevelTitleDbOpreation.java

public int insertOneRecord(FirstLevelTitle fTitle) { int result = 0; Connection con = null; PreparedStatement pStatement = null;

try { SimpleDateFormat HMFromat = new SimpleDateFormat(\hh:mm:ss\ String strCurrentTime = HMFromat.format( new Date()); con = ConnectionManager.getConnection(); String strSql =\ pStatement = con.prepareStatement(strSql); pStatement.setInt(1, getNewId()); pStatement.setString(2, fTitle.getTitleName()); pStatement.setString(3, fTitle.getCreator()); pStatement.setString(4, strCurrentTime); result = pStatement.executeUpdate(); } catch (SQLException sqlE) { sqlE.printStackTrace(); } finally { ConnectionManager.closeStatement(pStatement); ConnectionManager.closeConnection(con); } return result; }

删除一级标题:delFirstTitle.jsp <%

FirstLevelTitleDbOpreation fo=new FirstLevelTitleDbOpreation(); String firstLevelId=request.getParameter(\

fo.deleteFirstLevelTitle(Integer.parseInt(firstLevelId));

request.getRequestDispatcher(\ %> public void deleteFirstLevelTitle(int firstTitleId ){ Connection dbConnection = null; PreparedStatement pStatementS = null; PreparedStatement pStatementF = null; //ResultSet res = null; try { dbConnection = ConnectionManager.getConnection(); String delSecondSql = \ pStatementS = dbConnection.prepareStatement(delSecondSql); pStatementS.setInt(1, firstTitleId); pStatementS.executeUpdate(); String delFirstSql=\ pStatementF = dbConnection.prepareStatement(delFirstSql); pStatementF.setInt(1, firstTitleId); pStatementF.executeUpdate(); }catch(Exception e){ e.printStackTrace();

} }

更新一级标题:

’)”> <%

request.setCharacterEncoding(\

FirstLevelTitleDbOpreation fo=new FirstLevelTitleDbOpreation(); String firstLevelId=(String)request.getParameter(\ String titleName = request.getParameter(\ String Creator=request.getParameter(\

fo.updateFirstTitle(Integer.parseInt(firstLevelId),titleName,Creator);

request.getRequestDispatcher(\ %>

public void updateFirstTitle(int firstLevelId,String titlename,String creator){ Connection con = null; PreparedStatement pStatement = null; try { con = ConnectionManager.getConnection(); String strSql = \ pStatement = con.prepareStatement(strSql); pStatement.setString(1, titlename); pStatement.setString(2, creator); pStatement.setInt(3, firstLevelId); int row = pStatement.executeUpdate(); System.out.println(\成功更新了\行数据!\ } catch (SQLException sqlE) { sqlE.printStackTrace(); } finally { ConnectionManager.closeStatement(pStatement); ConnectionManager.closeConnection(con); } }

2.DAO技术的开发步骤 DAO由以下几部分组成:

①DatabaseConnection:专门负责数据库的打开与关闭操作的类。

②VO:主要由属性、setter、getter方法组成,VO类中的属性与表中的字段相对应,每一个VO类的对象都表示表中的每一条记录。

③DAO:主要定义操作的接口,定义一系列数据库的原子操作,如增加、修改、删除、按ID查询等。

④Impl:DAO接口的真实实现类,完成具体的数据库操作,但是不负责数据库的打开和关闭。 Vo类:

package cn.jmu.vo; public class Product {

//商品ID

protected int productID; //商品编号

protected String serialNumber; //商品名字

protected String name; //商品品牌

protected String brand; //商品型号

protected String model; //商品价格

protected double price; //商品的图片路径

protected String picture; //商品描述

protected String description; //商品无参构造 public Product() { super(); }

//商品有参构造

public Product(String serialNumber, String name, String brand, String model, double price, String picture, String description) { super(); this.serialNumber = serialNumber; this.name = name; this.brand = brand; this.model = model; this.price = price; this.picture = picture; this.description = description; }

public int getProductID() { return productID; }

public void setProductID(int productID) { this.productID = productID; }

public String getSerialNumber() { return serialNumber; }

public void setSerialNumber(String serialNumber) { this.serialNumber = serialNumber;