📄 bookbean.java
字号:
package com.j2ee14.ch17;import java.sql.*;import javax.naming.*;import javax.ejb.*;import java.util.*;/** * 演示 Bean-Managed Persistent Entity Bean. * 这个实体Bean代表了Book信息. */public class BookBean implements EntityBean { protected EntityContext ctx; // Bean管理的字段。 private String bookID; // PK private String bookName; private double price; public BookBean() { System.out.println("New Bank Book Entity Bean Java Object created by EJB Container."); } //业务逻辑方法 /** * 增加图书的价格. */ public void increasePrice(double amt) throws BookException { System.out.println("deposit(" + amt + ") called."); price += amt; } /** * 减少图书的价格 * @throw BookException thrown in amt < available price */ public void decreasePrice(double amt) throws BookException { System.out.println("withdraw(" + amt + ") called."); if (amt > price) { throw new BookException("the price is " + price + "! You cannot decrease " + amt + "!"); } price -= amt; } // getter和setter方法。 public double getPrice() { System.out.println("getBalance() called."); return price; } public void setBookName(String name) { System.out.println("setbookName() called."); bookName = name; } public String getBookName() { System.out.println("getbookName() called."); return bookName; } public String getBookID() { System.out.println("getBookID() called."); return bookID; } public void setBookID(String id) { System.out.println("setBookID() called."); this.bookID = id; } // // 以下是和EJB相关的方法。它们被容器调用。 // public void ejbActivate() { System.out.println("ejbActivate() called."); } //删除EJB,将删除数据库中对应的对象。 public void ejbRemove() throws RemoveException { System.out.println("ejbRemove() called."); BookPK pk = (BookPK) ctx.getPrimaryKey(); String id = pk.bookID; PreparedStatement pstmt = null; Connection conn = null; try { /* * 1) 获得JDBC连接 */ conn = getConnection(); /* * 2) 从数据库中删除book */ pstmt = conn.prepareStatement("delete from book where id = ?"); pstmt.setString(1, id); /* * 3)如果发生异常,则抛出 */ if (pstmt.executeUpdate() == 0) { throw new RemoveException("Book " + pk + " failed to be removed from the database"); } } catch (Exception ex) { throw new EJBException(ex.toString()); } finally { /* * 4) 释放数据库的连接 */ try { if (pstmt != null) pstmt.close(); } catch (Exception e) {} try { if (conn != null) conn.close(); } catch (Exception e) {} } } /** * 由容器调用,释放占有的资源。. */ public void ejbPassivate() { System.out.println("ejbPassivate () called."); } /** * 由容器调用,使得内存中的数据和数据库一致。 */ public void ejbLoad() { System.out.println("ejbLoad() called."); /* * Again, query the Entity Context to get the current * Primary Key, so we know which instance to load. */ BookPK pk = (BookPK) ctx.getPrimaryKey(); String id = pk.bookID; PreparedStatement pstmt = null; Connection conn = null; try { conn = getConnection(); pstmt = conn.prepareStatement("select bookName, price from book where id = ?"); pstmt.setString(1, id); ResultSet rs = pstmt.executeQuery(); rs.next(); bookName = rs.getString("bookName"); price = rs.getDouble("price"); } catch (Exception ex) { throw new EJBException("Book " + pk + " failed to load from database", ex); } finally { try { if (pstmt != null) pstmt.close(); } catch (Exception e) {} try { if (conn != null) conn.close(); } catch (Exception e) {} } } /** * 由容器调用,使得内存中的数据和数据库一致。 */ public void ejbStore() { System.out.println("ejbStore() called."); PreparedStatement pstmt = null; Connection conn = null; try { conn = getConnection(); pstmt = conn.prepareStatement("update book set bookName = ?, price = ? where id = ?"); pstmt.setString(1, bookName); pstmt.setDouble(2, price); pstmt.setString(3, bookID); pstmt.executeUpdate(); } catch (Exception ex) { throw new EJBException("Book " + bookID + " failed to save to database", ex); } finally { try { if (pstmt != null) pstmt.close(); } catch (Exception e) {} try { if (conn != null) conn.close(); } catch (Exception e) {} } } /** * 由容器调用设置Bean的上下文信息。 */ public void setEntityContext(EntityContext ctx) { System.out.println("setEntityContext called"); this.ctx = ctx; } /** * 由容器调用,取消上下文信息。 */ public void unsetEntityContext() { System.out.println("unsetEntityContext called"); this.ctx = null; } /** * 在ejbCreate()方法后调用。 */ public void ejbPostCreate(String bookID, String bookName) { } /** * 创建一个实体Bean。它和Home接口的create方法对应。 * @return The primary key for this book */ public BookPK ejbCreate(String bookID, String bookName) throws CreateException { PreparedStatement pstmt = null; Connection conn = null; try { System.out.println("ejbCreate() called."); this.bookID = bookID; this.bookName = bookName; this.price = 0; conn = getConnection(); //把数据添加到数据库 pstmt = conn.prepareStatement("insert into book (id, bookName, price) values (?, ?, ?)"); pstmt.setString(1, bookID); pstmt.setString(2, bookName); pstmt.setDouble(3, price); pstmt.executeUpdate(); /* *产生一个主键,然后返回。 */ return new BookPK(bookID); } catch (Exception e) { throw new CreateException(e.toString()); } finally { /* * 释放数据连接。 */ try { if (pstmt != null) pstmt.close(); } catch (Exception e) {} try { if (conn != null) conn.close(); } catch (Exception e) {} } } /** * 按照主键查找图书。 */ public BookPK ejbFindByPrimaryKey(BookPK key) throws FinderException { PreparedStatement pstmt = null; Connection conn = null; try { System.out.println("ejbFindByPrimaryKey(" + key + ") called"); /* *获得数据库连接。 */ conn = getConnection(); /* * 在数据库中查找实体。 */ pstmt = conn.prepareStatement("select id from book where id = ?"); pstmt.setString(1, key.toString()); ResultSet rs = pstmt.executeQuery(); rs.next(); /* *如果查找到,则返回主键。 */ return key; } catch (Exception e) { throw new FinderException(e.toString()); } finally { /* * 释放数据库连接。 */ try { if (pstmt != null) pstmt.close(); } catch (Exception e) {} try { if (conn != null) conn.close(); } catch (Exception e) {} } } /** * 按照图书的名字查找。 */ public Collection ejbFindByBookName(String name) throws FinderException { PreparedStatement pstmt = null; Connection conn = null; Vector v = new Vector(); try { System.out.println("ejbFindBybookName(" + name + ") called"); /* * 获得数据库连接 */ conn = getConnection(); /* * 按照图书的名字在数据库中查找。 */ pstmt = conn.prepareStatement("select id from book where bookName = ?"); pstmt.setString(1, name); ResultSet rs = pstmt.executeQuery(); /* * 把每个匹配的记录放在Vector中。 */ while (rs.next()) { String id = rs.getString("id"); v.addElement(new BookPK(id)); } /* *返回查找的结果 */ return v; } catch (Exception e) { throw new FinderException(e.toString()); } finally { /* * 释放数据库连接。 */ try { if (pstmt != null) pstmt.close(); } catch (Exception e) {} try { if (conn != null) conn.close(); } catch (Exception e) {} } } /** * 从连接池中获得一个连接。 * * @return The JDBC connection */ public Connection getConnection() throws Exception { try { Context ctx = new InitialContext(); javax.sql.DataSource ds = (javax.sql.DataSource) ctx.lookup("java:comp/env/jdbc/ejbPool"); return ds.getConnection(); } catch (Exception e) { System.err.println("Could not locate datasource! Reason:"); e.printStackTrace(); throw e; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -