510851117sy08.docx
《510851117sy08.docx》由会员分享,可在线阅读,更多相关《510851117sy08.docx(27页珍藏版)》请在冰豆网上搜索。
![510851117sy08.docx](https://file1.bdocx.com/fileroot1/2023-6/23/daf258ac-8072-4b2c-b018-af14344aab2c/daf258ac-8072-4b2c-b018-af14344aab2c1.gif)
510851117sy08
淮海工学院计算机工程学院
实验报告书
课程名:
J2EE应用开发
题目:
实验八购物车功能的实现
班级:
学号:
姓名:
实验八购物车功能的实现
1.目的与要求
1)使用JDBC操纵数据库
2)使用Hbernate操纵数据库
3)JDBC驱动程序文档
2.实验内容
1使用JDBC开发用户注册的程序并将注册信息加入到数据库中
2使用Hibernate开发用户注册程序并与数据库交换数据。
3.实验步骤
4.测试数据与实验结果
5.实验体会:
packagecom.un.mybookstore.model;
importjava.util.HashMap;
importjava.util.Iterator;
importjava.util.Map;
importcom.un.mybookstore.vo.Book;
importcom.un.mybookstore.vo.Orderitem;
publicclassCart{
protectedMapitems;
publicCart(){
if(items==null){
items=newHashMap();
}
}
publicvoidaddBook(Integerbookid,Orderitemorderitem){
if(items.containsKey("bookid")){
Orderitem_orderitem=items.get(bookid);
_orderitem.setQuantity(_orderitem.getOrderitemid()+orderitem.getQuantity());
items.put(bookid,_orderitem);
}
else{
items.put(bookid,orderitem);
}
}
publicvoidupdateCart(Integerbookid,intquantity){
Orderitemorderitem=items.get(bookid);
orderitem.setQuantity(quantity);
items.put(bookid,orderitem);
}
publicintgetTotalPrice(){
inttotalPrice=0;
for(Iteratorit=items.values().iterator();it.hasNext();){
Orderitemorderitem=(Orderitem)it.next();
Bookbook=orderitem.getBook();
intquantity=orderitem.getQuantity();
totalPrice+=book.getPrice()*quantity;
}
returntotalPrice;
}
publicMapgetItems(){
returnitems;
}
publicvoidsetItems(Mapitems){
this.items=items;
}
}
xmlversion="1.0"encoding="utf-8"?
>
DOCTYPEhibernate-mappingPUBLIC"-//Hibernate/HibernateMappingDTD3.0//EN"
"
--
MappingfileautogeneratedbyMyEclipsePersistenceTools
-->
xmlversion="1.0"encoding="utf-8"?
>
DOCTYPEhibernate-mappingPUBLIC"-//Hibernate/HibernateMappingDTD3.0//EN"
"
--
MappingfileautogeneratedbyMyEclipsePersistenceTools
-->
packagecom.un.mybookstore.vo;
importjava.util.Date;
importjava.util.HashSet;
importjava.util.Set;
/**
*Ordersentity.
*
*@authorMyEclipsePersistenceTools
*/
publicclassOrdersimplementsjava.io.Serializable{
//Fields
privateIntegerorderid;
privateUseruser;
privateDateorderdate;
privateSetorderitems=newHashSet(0);
//Constructors
/**defaultconstructor*/
publicOrders(){
}
/**fullconstructor*/
publicOrders(Useruser,Dateorderdate,Setorderitems){
this.user=user;
this.orderdate=orderdate;
this.orderitems=orderitems;
}
//Propertyaccessors
publicIntegergetOrderid(){
returnthis.orderid;
}
publicvoidsetOrderid(Integerorderid){
this.orderid=orderid;
}
publicUsergetUser(){
returnthis.user;
}
publicvoidsetUser(Useruser){
this.user=user;
}
publicDategetOrderdate(){
returnthis.orderdate;
}
publicvoidsetOrderdate(Dateorderdate){
this.orderdate=orderdate;
}
publicSetgetOrderitems(){
returnthis.orderitems;
}
publicvoidsetOrderitems(Setorderitems){
this.orderitems=orderitems;
}
}
packagecom.un.mybookstore.vo;
/**
*Orderitementity.
*
*@authorMyEclipsePersistenceTools
*/
publicclassOrderitemimplementsjava.io.Serializable{
//Fields
privateIntegerorderitemid;
privateBookbook;
privateOrdersorders;
privateIntegerquantity;
//Constructors
/**defaultconstructor*/
publicOrderitem(){
}
/**minimalconstructor*/
publicOrderitem(Integerquantity){
this.quantity=quantity;
}
/**fullconstructor*/
publicOrderitem(Bookbook,Ordersorders,Integerquantity){
this.book=book;
this.orders=orders;
this.quantity=quantity;
}
//Propertyaccessors
publicIntegergetOrderitemid(){
returnthis.orderitemid;
}
publicvoidsetOrderitemid(Integerorderitemid){
this.orderitemid=orderitemid;
}
publicBookgetBook(){
returnthis.book;
}
publicvoidsetBook(Bookbook){
this.book=book;
}
publicOrdersgetOrders(){
returnthis.orders;
}
publicvoidsetOrders(Ordersorders){
this.orders=orders;
}
publicIntegergetQuantity(){
returnthis.quantity;
}
publicvoidsetQuantity(Integerquantity){
this.quantity=quantity;
}
}
packagecom.un.mybookstore.dao;
importjava.util.List;
importcom.un.mybookstore.vo.Book;
publicinterfaceIBookDAO{
publicListgetBookByCatalogid(Integercatalogid);
publicListgetBookByCatalogidPaging(Integercatalogid,intcurrentPage,intpageSize);
publicintgetTotalByCatalog(Integercatalogid);
publicBookgetBookById(Integerbookid);
publicListgetRequiredBookByHql(Stringhql);
publicListgetNewBook();
}
packagecom.un.mybookstore.dao.impl;
importjava.util.List;
importcom.un.mybookstore.dao.BaseDAO;
importcom.un.mybookstore.dao.IBookDAO;
importcom.un.mybookstore.vo.Book;
importorg.hibernate.Query;
importorg.hibernate.classic.Session;
publicclassBookDAOextendsBaseDAOimplementsIBookDAO{
protectedIntegercatalogid;
publicIntegergetCatalogid(){
returncatalogid;
}
publicvoidsetCatalogid(Integercatalogid){
this.catalogid=catalogid;
}
publicListgetBookByCatalogid(Integercatalogid){
Sessionsession=getSession();
Stringhql="fromBookbwhereb.catalog.catalogid=?
";
Queryquery=session.createQuery(hql);
query.setParameter(0,catalogid);
Listbooks=query.list();
session.close();
returnbooks;
}
publicListgetBookByCatalogidPaging(Integercatalogid,intcurrentPage,intpageSize){
Sessionsession=getSession();
Queryquery=session.createQuery("fromBookbwhereb.catalog.catalogid=?
");
query.setParameter(0,catalogid);
intstartRow=(currentPage-1)*pageSize;
query.setFirstResult(startRow);
query.setMaxResults(pageSize);
Listbooks=query.list();
session.close();
returnbooks;
}
publicListgetNewBook(){
Sessionsession=getSession();
Queryquery=session.createQuery("fromBookb");
query.setFirstResult(0);
query.setMaxResults(5);
Listbooks=query.list();
session.close();
returnbooks;
}
publicintgetTotalByCatalog(Integercatalogid){
Sessionsession=getSession();
Queryquery=session.createQuery("fromBookbwhereb.catalog.catalogid=?
");
query.setParameter(0,catalogid);
Listbooks=query.list();
inttotalSize=books.size();
session.close();
returntotalSize;
}
publicBookgetBookById(Integerbookid){
Sessionsession=getSession();
Bookbook=(Book)session.get(Book.class,bookid);
session.close();
returnbook;
}
publicListgetRequiredBookByHql(Stringhql){
try{
Sessionsession=getSession();
Queryquery=session.createQuery(hql);
Listbooks=query.list();
System.out.println(hql);
returnbooks;
}catch(Exceptione){
System.out.println(e.getStackTrace());
returnnull;
}
}
}
packagecom.un.mybookstore.service;
importjava.util.List;
importcom.un.mybookstore.vo.Book;
publicinterfaceIBookService{
publicListgetBookByCatalogid(Integercatalogid);
publicListgetBookByCatalogidPaging(Integercatalogid,intcurrentPage,intpageSize);
publicintgetTotalByCatalog(Integercatalogid);
publicBookgetBookById(Integerbookid);
publicListgetRequiredBookByHql(Stringhql);
publicListgetNewBook();
}
packagecom.un.mybookstore.service.impl;
importjava.util.List;
importcom.un.mybookstore.dao.IBookDAO;
importcom.un.mybookstore.service.IBookService;
importcom.un.mybookstore.vo.Book;
publicclassBookServiceimplementsIBookService{
protectedIBookDAObookDAO;
publicListgetBookByCatalogid(Integercatalogid){
returnbookDAO.getBookByCatalogid(catalogid);
}
publicListgetBookByCatalogidPaging(Integercatalogid,intcurrentPage,intpageSize){
returnbookDAO.getBookByCatalogidPaging(catalogid,currentPage,pageSize);
}
publicintgetTotalByCatalog(Integercatalogid){
returnbookDAO.getTotalByCatalog(catalogid);
}
publicIBookDAOgetBookDAO(){
returnbookDAO;
}
publicvoidsetBookDAO(IBookDAObookDAO){
this.bookDAO=bookDAO;
}
publicBookgetBookById(Integerbookid){
returnbookDAO.getBookById(bookid);
}
publicListgetRequiredBookByHql(Stringhql){
returnbookDAO.getRequiredBookByHql(hql);
}
publicListgetNewBook(){
returnbookDAO.getNewBook();
}
}
packagecom.un.mybookstore.service;
importcom.un.mybookstore.vo.Orders;
publicinterfaceIOrderService{
public