//引入相关包
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)
{
}
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
Java完全自学宝典源代码.rar
共726个文件
java:274个
class:253个
html:87个
需积分: 10 7 下载量 193 浏览量
2011-07-27
15:35:37
上传
评论
收藏 1.49MB RAR 举报
温馨提示
Java完全自学宝典源代码.rar Java完全自学宝典源代码.rar Java完全自学宝典源代码.rar Java完全自学宝典源代码.rar
资源推荐
资源详情
资源评论
收起资源包目录
Java完全自学宝典源代码.rar (726个子文件)
DeleteStudent.class 4KB
DBBean.class 3KB
DBBean.class 3KB
Example.class 3KB
CardLayoutEx.class 2KB
DBBean.class 2KB
DBBean.class 2KB
Cart.class 2KB
Cart.class 2KB
GridLayoutEx.class 2KB
JdbcText1.class 1KB
studentInfo.class 1KB
Info.class 1KB
StudentInfo.class 1KB
usingdoGet.class 1KB
WindowListenerEx.class 1KB
WindowListenerEx.class 1KB
FlowLayoutEx2.class 1KB
InfoData.class 1KB
usingdoPost.class 1KB
WangLuo5.class 1KB
WangLuo4.class 1KB
TextAreaEx.class 1KB
TextAreaEx.class 1KB
Usedata.class 1KB
ButtonEx.class 1KB
ButtonEx.class 1KB
XianCheng6.class 1KB
ServerThread.class 1KB
YiChang6.class 1KB
ShangDian.class 1KB
Example.class 1KB
Example.class 1KB
text.class 1KB
Example1.class 1KB
Good1.class 1KB
Good1.class 1KB
Data.class 1KB
Data.class 1KB
Data.class 1KB
MyThread.class 1KB
Good.class 1KB
Welcome.class 1003B
Book.class 998B
Book.class 998B
Usebuffer.class 991B
Example.class 977B
Example2.class 973B
WangLuo3.class 973B
Example2.class 956B
WangLuo6.class 933B
Usepri.class 923B
Student1.class 916B
Example.class 904B
NullEx.class 879B
School1.class 879B
Rect.class 875B
Usesta.class 869B
Usefile.class 869B
text.class 863B
WangLuo2.class 858B
SFoo.class 856B
MyResource.class 854B
text.class 852B
Imp1.class 831B
Imp1.class 831B
School.class 821B
School.class 821B
YiChang8.class 821B
UseFileOutputStream.class 820B
Usebuffer1.class 810B
YiChang7.class 807B
Example1.class 806B
Useext.class 806B
FlowLayoutEx.class 802B
Usefile.class 794B
Example1.class 787B
YiChang4.class 785B
Abs3.class 779B
Example1.class 774B
text.class 770B
YiChang3.class 769B
text.class 765B
Usepro.class 764B
Example.class 751B
Example$Example1.class 742B
Usepri$text.class 742B
Usepri$text.class 742B
Usepri$text.class 742B
Usebuffer2.class 735B
Usebuffer.class 733B
Example.class 728B
Example.class 697B
text.class 696B
Example.class 686B
Example1.class 686B
UseFileInputStream.class 685B
Usefile.class 683B
YiChang9.class 681B
Usefin.class 679B
共 726 条
- 1
- 2
- 3
- 4
- 5
- 6
- 8
资源评论
专业托
- 粉丝: 0
- 资源: 50
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功