?? bmpbookbean.java
字號:
//引入相關(guān)包
import java.util.*;
import javax.ejb.*;
import javax.naming.*;
//這里需要引入sql處理包
import java.sql.*;
import javax.sql.*;
//BMP實體EJB的組件類
public class BmpBookBean implements EntityBean
{
//映射isbn 字段
private String bookIsbn;
//映射author 字段
private String bookAuthor;
//映射title 字段
private String bookTitle;
//映射price 字段
private double bookPrice;
private EntityContext ec;
private DataSource ds;
private Connection con;
private String jndi ="bmpbook";
//實現(xiàn)業(yè)務(wù)方法
public void setBookAuthor(String bookAuthor)
{
this.bookAuthor=bookAuthor;
}
public void setBookTitle(String bookTitle)
{
this.bookTitle=bookTitle;
}
public void setBookPrice(double bookPrice)
{
this.bookPrice=bookPrice;
}
public String getIsbn()
{
return this.bookIsbn;
}
public String getBookAuthor( )
{
return this.bookAuthor;
}
public String getBookTitle( )
{
return this.bookTitle;
}
public double getBookPrice( )
{
return this.bookPrice;
}
//實現(xiàn)接口中的setEntityContext方法
public void setEntityContext(EntityContext ec)
{
try
{
Context initial =new InitialContext();
ds=(DataSource)initial.lookup(jndi);
}
catch(NamingException ne)
{
throw new EJBException(ne);
}
}
//實現(xiàn)數(shù)據(jù)的插入
public String ejbCreate(String isbn,String author,String title,double price) throws CreateException
{
if(bookIsbn==null)
{
throw new CreateException("主鍵不能為空");
}
try
{
String sql = "insert into book values(?,?,?,?)";
con = ds.getConnection();
PreparedStatement stmt = con.prepareStatement(sql);
stmt.setString(1,bookIsbn);
stmt.setString(2,bookAuthor);
stmt.setString(3,bookTitle);
stmt.setDouble(4,bookPrice);
stmt.executeUpdate();
stmt.close();
}
catch (SQLException e)
{
throw new EJBException(e);
}
finally
{
try
{
if (con!=null)
{
con.close();
}
}
catch (SQLException e)
{
}
}
this.bookIsbn=bookIsbn;
this.bookAuthor=bookAuthor;
this.bookTitle=bookTitle;
this.bookPrice=bookPrice;
//返回主鍵值
return bookIsbn;
}
public void ejbPostCreate(String isbn,String author,String title,double price)
{
}
//實現(xiàn)數(shù)據(jù)的查找
public void ejbLoad()
{
try
{
String sql="select isbn,author,title,price from book where isbn=?";
con=ds.getConnection();
PreparedStatement stmt =con.prepareStatement(sql);
stmt.setString(1,this.bookIsbn);
ResultSet rset=stmt.executeQuery();
if(rset.next())
{
this.bookAuthor=rset.getString("author");
this.bookTitle=rset.getString("title");
this.bookPrice=rset.getDouble("price");
stmt.close();
}
else
{
stmt.close();
throw new NoSuchEntityException("書的Isbn為:"+this.bookIsbn);
}
}
catch (SQLException se)
{
throw new EJBException(se);
}
finally
{
try{
if(con!=null)
{
con.close();
}
}
catch(SQLException se)
{
}
}
}
//實現(xiàn)數(shù)據(jù)的修改
public void ejbStore()
{
try
{
String sql="update book set author=?,title=?,price=? where isbn=?";
con=ds.getConnection();
PreparedStatement stmt =con.prepareStatement(sql);
stmt.setString(1,this.bookAuthor);
stmt.setString(2,this.bookTitle);
stmt.setDouble(3,this.bookPrice);
stmt.setString(4,this.bookIsbn);
if(stmt.executeUpdate()!=1)
{
stmt.close();
throw new EJBException("修改異常");
}
stmt.close();
}
catch (SQLException se)
{
throw new EJBException(se);
}
finally
{
try
{
if(con!=null)
{
con.close();
}
}
catch(SQLException se)
{
}
}
}
//實現(xiàn)數(shù)據(jù)的刪除
public void ejbRemove()
{
try
{
String sql="delete from book where isbn=?";
con=ds.getConnection();
PreparedStatement stmt =con.prepareStatement(sql);
stmt.setString(1,this.bookIsbn);
if(stmt.executeUpdate()!=1)
{
throw new EJBException("刪除異常");
}
stmt.close();
}
catch (SQLException se)
{
throw new EJBException(se);
}
finally
{
try{
if(con!=null)
{
con.close();
}
}
catch(SQLException se)
{
}
}
}
public void unsetEntityContext()
{
this.ec=null;
}
public void ejbActivate()
{
this.bookIsbn=(String)ctx.getPrimaryKey();
}
public void ejbPassivate()
{
this.bookIsbn=null;
}
public String ejbFindByPrimaryKey(String primarykey) throws FinderException
{
try
{
String sql="select isbn from book where isbn=?";
con = ds.getConnection();
PreparedStatement stmt =con.prepareStatement(sql);
stmt.setString(1,primarykey);
ResultSet rset=stmt.executeQuery();
if(!rset.next())
{
stmt.close();
throw new ObjectNotFoundException();
}
stmt.close();
return primarykey;
}
catch (SQLException se)
{
throw new EJBException(se);
}
finally
{
try{
if(con!=null)
{
con.close();
}
}
catch(SQLException se)
{
}
}
}
public Collection ejbFindByPrice(double price) throws FinderException
{
try
{
String sql="select isbn from book where price >?";
con=ds.getConnection();
PreparedStatement stmt =con.prepareStatement(sql);
stmt.setDouble(1,price);
ResultSet rset=stmt.executeQuery();
ArrayList booklist=new ArrayList();
while(rset.next())
{
booklist.add(rset.getString("id"));
}
stmt.close();
return booklist;
}
catch (SQLException se)
{
throw new EJBException(se);
}
finally
{
try
{
if(con!=null)
{
con.close();
}
}
catch(SQLException se)
{
}
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -