BMP数据更新问题

fccfcc1234 2006-06-21 03:03:08
初学EJB,自己写的BMP不知道为什么数据库的数据就是不能更新.ejbStore()方法就是不调用.各位大虾帮忙看下啊.
package com.ejb;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.EntityBean;
import javax.ejb.EntityContext;
import javax.ejb.FinderException;
import javax.ejb.NoSuchEntityException;
import javax.ejb.ObjectNotFoundException;
import javax.ejb.RemoveException;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

/**
*
* <!-- begin-user-doc --> You can insert your documentation for '<em><b>TestBMPBean</b></em>'. <!-- end-user-doc --> *
<!-- begin-lomboz-definition -->
<?xml version="1.0" encoding="UTF-8"?>
<lomboz:EJB xmlns:j2ee="http://java.sun.com/xml/ns/j2ee" xmlns:lomboz="http://lomboz.objectlearn.com/xml/lomboz">
<lomboz:entity>
<lomboz:entityEjb>
<j2ee:display-name>TestBMP</j2ee:display-name>
<j2ee:ejb-name>TestBMP</j2ee:ejb-name>
<j2ee:ejb-class>com.ejb.TestBMPBean</j2ee:ejb-class>
<j2ee:persistence-type>Bean</j2ee:persistence-type>
<j2ee:cmp-version>2.x</j2ee:cmp-version>
<j2ee:abstract-schema-name>mySchema</j2ee:abstract-schema-name>
</lomboz:entityEjb>
<lomboz:tableName></lomboz:tableName>
<lomboz:dataSourceName></lomboz:dataSourceName>
</lomboz:entity>
</lomboz:EJB>
<!-- end-lomboz-definition -->
*
* <!-- begin-xdoclet-definition -->



/**
* @ejb.bean name="TestBMP"
* jndi-name="TestBMP"
* type="BMP"
*
* <!-- end-xdoclet-defintion -->
* @generated
**/
public abstract class TestBMPBean implements javax.ejb.EntityBean {
private String id;
private Double price;

private EntityContext ctx;
private DataSource ds;
private String dbjndi = "java:/MySqlDS";
private Connection con;
public String getId() {
System.out.println("getid");
return id;
}

public void setId(String id) {
System.out.println("setid");
this.id = id;
}

public Double getPrice() {
System.out.println("getprice");
return price;
}

public void setPrice(Double price) {
System.out.println("setprice");
this.price = price;
}

/**
*
* <!-- begin-user-doc -->
* The ejbCreate method.
* <!-- end-user-doc -->
*
* <!-- begin-xdoclet-definition -->
* @ejb.create-method
* <!-- end-xdoclet-definition -->
* @generated
*/
public java.lang.String ejbCreate() throws javax.ejb.CreateException {
// EJB 2.0 spec says return null for CMP ejbCreate methods.
// TODO: YOU MUST INITIALIZE THE FIELDS FOR THE BEAN HERE.
// setMyField("Something");
// begin-user-code
return null;
// end-user-code
}

/**
* <!-- begin-user-doc -->
* The container invokes this method immediately after it calls ejbCreate.
* <!-- end-user-doc -->
*
* @generated
*/
public void ejbPostCreate() throws javax.ejb.CreateException {
// begin-user-code
// end-user-code
}

/**
* @ejb.create-method
* view-type="remote"
**/
public String ejbCreate(String id,Double price)throws CreateException{
System.out.println("ejbCreate");
if(id==null)
throw new CreateException("The id is required");
try{
String sql = "insert into item values('"+id+"','"+price+"')";
con = ds.getConnection();
PreparedStatement stmt = con.prepareStatement(sql);
stmt.executeUpdate();
stmt.close();
}catch(SQLException se){
throw new EJBException(se);
}finally{
try{
if(con!=null)con.close();
}catch(SQLException se){}
}
setId(id);
setPrice(price);
return id;
}
/**
* @ejb.create-method
* view-type="remote"
**/
public void ejbActivate(){
System.out.println("ejbActivate");
this.id = (String)ctx.getPrimaryKey();
}
/**
* @throws RemoveException
* @ejb.create-method
* view-type="remote"
**/
public void ejbRemove() throws RemoveException{
System.out.println("ejbRemove");
try{
String sql = "delete from item where itemid = ?";
con = ds.getConnection();
PreparedStatement stmt = con.prepareStatement(sql);
stmt.setString(1,this.id);
if(stmt.executeUpdate()!=1)
throw new RemoveException("occount a error on remove");
stmt.close();
}catch(SQLException se){
throw new EJBException(se);
}finally{
try{
if(con!=null)
con.close();
}catch(SQLException se){}
}
}
/**
* @ejb.create-method
* view-type="remote"
**/
public void ejbPassivate(){
System.out.println("ejbPassivate");
this.id = null;
}
/**
* @ejb.create-method
* view-type="remote"
**/
public void ejbStore(){
System.out.println("ejbStore");
try{
String sql = "update item set itemid = '"+this.id+"',price = "+this.price+" where itemid = '"+this.id+"'";
con = ds.getConnection();
PreparedStatement stmt = con.prepareStatement(sql);
if(stmt.executeUpdate()!=1){
stmt.close();
throw new EJBException("occount a error on saved");
}
stmt.close();
}catch(SQLException se){
throw new EJBException(se);
}finally{
try{
if(con!=null)
con.close();
}catch(SQLException se){}
}
}
/**
* @ejb.create-method
* view-type="remote"
**/
public void ejbLoad(){
System.out.println("ejbLoad");
try{
System.out.println(this.id);
String sql = "select itemid,price from item where itemid = '"+this.id+"'";
con = ds.getConnection();
PreparedStatement stmt = con.prepareStatement(sql);
ResultSet rest = stmt.executeQuery();
if(rest.next()){
this.id = rest.getString("itemid");
this.price = Double.valueOf(rest.getString("price")).doubleValue();
stmt.close();
}else{
stmt.close();
throw new NoSuchEntityException("BOOK ID:" + this.id);
}
}catch(SQLException se){
throw new EJBException(se);
}finally{
try{
if(con!=null)con.close();
}catch(SQLException se){}
}
}
/**
* @ejb.create-method
* view-type="remote"
**/
public void setEntityContext(EntityContext context){
System.out.println("setEntityContext");
this.ctx = context;
try{
InitialContext initial = new InitialContext();
ds = (DataSource)initial.lookup(this.dbjndi);
}catch(NamingException ne){
throw new EJBException(ne);
}
}
/**
* @ejb.create-method
* view-type="remote"
**/
public void unsetEntityContext(){
System.out.println("unsetEntityContext");
this.ctx = null;
}
/**
* @ejb.create-method
* view-type="remote"
**/
public void ejbPostCreate(String id,Double price)throws CreateException{
System.out.println("ejbPostCreate");
}
/**
* @ejb.interface-method
* view-type="remote"
**/
public void deposit(double amt)throws Exception{
System.out.println("deposit");
price = 200.00;
}
/**
* @ejb.interface-method
* view-type="remote"
**/
public void withdraw(double amt) throws Exception{
System.out.println("withdraw");
price = 300.00;
}
/**
* @ejb.create-method
* view-type="remote"
**/
public String ejbFindByPrimaryKey(String primarykey) throws FinderException{
System.out.println("ejbFindByPrimaryKey");
try{
String sql = "select itemid from item where itemid = '"+primarykey+"'";
con = ds.getConnection();
PreparedStatement stmt = con.prepareStatement(sql);
ResultSet rset = stmt.executeQuery();
if(!rset.next()){
stmt.close();
throw new ObjectNotFoundException();
}
stmt.close();
id = primarykey;
return primarykey;
}catch(SQLException se){
throw new EJBException(se);
}finally{
try{
if(con!=null)
con.close();
}catch(SQLException se){}
}
}
}
...全文
160 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
eqxu 2006-06-23
  • 打赏
  • 举报
回复
根一下代码不就知道了
别人怎么跟阿
rainstar145 2006-06-23
  • 打赏
  • 举报
回复
打开Eclipse Debug跟下代码怎么走的就知道了
如果你的代码能执行ejbLoad()而不执行ejbStore那就是容器问题了。。
呵呵
weichenggao 2006-06-23
  • 打赏
  • 举报
回复
bmp,cmp这些现在都不怎么用了吧!
fccfcc1234 2006-06-22
  • 打赏
  • 举报
回复
帮忙看下吧.
fccfcc1234 2006-06-21
  • 打赏
  • 举报
回复
知道的说下吧.
fccfcc1234 2006-06-21
  • 打赏
  • 举报
回复
95%自己写的.我用Eclipse+lomboz的.
jrunner 2006-06-21
  • 打赏
  • 举报
回复
楼主你不会用手打的代码吧?干吗贴这么多呢,JBuilder生成了80%,你就把你打的20%发上来就可以了!

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧