📄 bmpbookbean.java
字号:
//引入相关包
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";
//实现业务方法
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;
}
//实现接口中的setEntityContext方法
public void setEntityContext(EntityContext ec)
{
try
{
Context initial =new InitialContext();
ds=(DataSource)initial.lookup(jndi);
}
catch(NamingException ne)
{
throw new EJBException(ne);
}
}
//实现数据的插入
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)
{
}
//实现数据的查找
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)
{
}
}
}
//实现数据的修改
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)
{
}
}
}
//实现数据的删除
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)
{
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -