java ee5 tutorial - 数据库的访问

实体bean,可以IDE产生也可以手写:
package  webtest.bookstore.database;

import  java.io.Serializable;
import  javax.persistence.Entity;
import  javax.persistence.Id;
import  javax.persistence.Table;

@Entity
@Table(name 
=   " WEB_BOOKSTORE_BOOKS " )
public   class  Book  implements  Serializable  {
    
private String bookId;
    
private String description;
    
private String firstName;
    
private String surname;
    
private String title;
    
private boolean onSale;
    
private float price;
    
private int calendar_year;
    
private int inventory;

    
public Book() {
    }


    
public Book(
        String bookId,
        String surname,
        String firstName,
        String title,
        
float price,
        
boolean onSale,
        
int calendar_year,
        String description,
        
int inventory) {
        
this.bookId = bookId;
        
this.title = title;
        
this.firstName = firstName;
        
this.surname = surname;
        
this.price = price;
        
this.onSale = onSale;
        
this.calendar_year = calendar_year;
        
this.description = description;
        
this.inventory = inventory;
    }


    @Id
    
public String getBookId() {
        
return this.bookId;
    }


    
public String getTitle() {
        
return this.title;
    }


    
public String getFirstName() {
        
return this.firstName;
    }


    
public String getSurname() {
        
return this.surname;
    }


    
public float getPrice() {
        
return this.price;
    }


    
public boolean getOnSale() {
        
return this.onSale;
    }


    
public int getCalendar_year() {
        
return this.calendar_year;
    }


    
public String getDescription() {
        
return this.description;
    }


    
public int getInventory() {
        
return this.inventory;
    }


    
public void setBookId(String id) {
        
this.bookId = id;
    }


    
public void setTitle(String title) {
        
this.title = title;
    }


    
public void setFirstName(String firstName) {
        
this.firstName = firstName;
    }


    
public void setSurname(String surname) {
        
this.surname = surname;
    }


    
public void setPrice(float price) {
        
this.price = price;
    }


    
public void setOnSale(boolean onSale) {
        
this.onSale = onSale;
    }


    
public void setCalendar_year(int calendar_year) {
        
this.calendar_year = calendar_year;
    }


    
public void setDescription(String description) {
        
this.description = description;
    }


    
public void setInventory(int inventory) {
        
this.inventory = inventory;
    }

}


数据库的操作,使用facade模式:
package  webtest.bookstore2.database;

import  java.util.Collection;
import  java.util.ArrayList;
import  java.util.List;

import  webtest.bookstore.cart. * ;
import  webtest.bookstore.exception. * ;
import  webtest.bookstore.database. * ;

import  javax.persistence.EntityManagerFactory;
import  javax.persistence.EntityManager;

public   class  BookDBAO  {
    
private ArrayList books;
    
private EntityManager em;
    
/** Creates a new instance of BookDBAO */
    
public BookDBAO(EntityManagerFactory emf)throws Exception {
        
try{
            em
=emf.createEntityManager();
        }
catch(Exception e){
            
throw new Exception("  Could not connection to database: "+e.getMessage()+" ");
        }

    }

    
    
public void remove() {
        
try {
            em.close();
        }
 catch (Exception ex) {
            System.out.println(
" "+ex.getMessage()+" ");
        }

    }

    
    
public List getBooks()throws BooksNotFoundException{
        
try{
            
return em.createQuery("SELECT bd FROM Book bd ORDER BY bd.bookId").getResultList();
        }
catch(Exception e){
            
throw new BooksNotFoundException("  Could not get all the books: "+ e.getMessage()+" ");
        }

    }

    
    
public Book getBook(String id)throws BookNotFoundException{
        Book book
=em.find(Book.class,id);
        
if(book==null){
            
throw new BookNotFoundException("  Could not get the book: "+ id+" ");
        }

        
return book;
    }

    
    
public void buyBooks(ShoppingCart cart)throws OrderException{
        
try{
            
for(ShoppingCartItem sci:cart.getItems()){
                Book book
=(Book)sci.getItem();
                String id
=book.getBookId();
                
int quantity=sci.getQuantity();
                
this.buyBook(id,quantity);
            }

        }
catch(Exception e){
            
throw new OrderException(" Commit failed: " + e.getMessage()+" ");
        }

    }

    
    
public void buyBook(String bookId,int quantity) throws OrderException {
        
try{
            Book book
=em.find(Book.class,bookId);
            
if(book!=null){
                
int inventory = book.getInventory();
                
if ((inventory - quantity) >= 0{
                    
int newInventory = inventory - quantity;
                    book.setInventory(newInventory);
                }
 else {
                    
throw new OrderException(" Not enough of " + bookId
                            
+ " in stock to complete order. ");
                }

            }

        }
catch(Exception e){
            
throw new OrderException(" Couldn't purchase book: " + bookId + e.getMessage()+" ");
        }

    }

}


在Context中使用:
import  webtest.bookstore2.database.BookDBAO;

public   class  ContextListener  implements  ServletContextListener  {
    @PersistenceUnit
    
private EntityManagerFactory emf;
    
private ServletContext context = null;
    
    
public void contextInitialized(ServletContextEvent evt) {
        context
=evt.getServletContext();
        
try {
            BookDBAO bookDBAO 
= new BookDBAO(emf);
            context.setAttribute(
"bookDBAO", bookDBAO);
        }
 catch (Exception ex) {
            System.out.println(
"Couldn't create bookstore database bean: "
                    
+ ex.getMessage());
        }

    }


    
    
public void contextDestroyed(ServletContextEvent evt) {
        context 
= evt.getServletContext();
        BookDBAO bookDBAO 
= (BookDBAO) context.getAttribute("bookDBAO");
        
if (bookDBAO != null{
            bookDBAO.remove();
        }

        context.removeAttribute(
"bookDBAO");
    }

}

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值