OrdersPK class extends java.io.*;序列化 public OrdersPK ejbCreate(java.lang.Integer order_id,java.lang.Integer product_no,java.lang.Integer quantity) throws CreateException{PreparedStatement pstmt=null; Connection conn=null; try{ conn=getConnection(); pstmt=conn.prepareStatement("insert into orders (order_id,product_no,quantity) values(?,?,?)");this.order_id=order_id;this.product_no=product_no;this.quantity=quantity;pstmt.setInt(1,order_id.intValue());pstmt.setInt(2,product_no.intValue());pstmt.setInt(3,quantity.intValue());pstmt.executeUpdate();return new OrdersPK(order_id);}catch (SQLException sqle){ throw new EJBException(sqle); }finally{ try{ if (pstmt!=null) { pstmt.close(); } if (conn!=null) { conn.close(); } }catch(SQLException sqle){}
} }public OrdersPK ejbFindByPrimaryKey(OrdersPK key) throws FinderException{ System.out.println("ejbFindByPrimaryKey() called"); PreparedStatement pstmt=null; Connection conn=null; try{ conn=getConnection();pstmt=conn.prepareStatement("select order_id from orders where order_id=?"); pstmt.setInt(1,key.order_id.intValue());ResultSet rs=pstmt.executeQuery(); if (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 void ejbLoad(){ System.out.println("ejbLoad() called"); OrdersPK key=(OrdersPK)ctx.getPrimaryKey(); System.out.println("key is"+key.toString()); java.lang.Integer order_id=key.order_id; PreparedStatement pstmt=null; Connection conn=null; try{ conn=getConnection();pstmt=conn.prepareStatement("select order_id,product_no,quantity from orders where order_id=?"); pstmt.setInt(1,key.order_id.intValue()); ResultSet rs=pstmt.executeQuery(); rs.next(); order_id=rs.getString("order_id"); product_no=new Integer(rs.getInt("product_no")); System.out.println("**load:product_no is"+product_no.toString()); quantity=new Integer(rs.getInt("quantity")); }catch(Exception ex){ throw new EJBException("failed to load from the database"+ex); }finally{System.out.println("order_id is"+order_id.toString()); 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 orders set product_no=?,quantity=? where order_id=?"); pstmt.setInt(1,product_no.intValue());
pstmt.setInt(2,quantity.intValue());
pstmt.setInt(3,order_id.intValue());
pstmt.executeUpdate(); }catch(Exception ex){ throw new EJBException("failed to save to the database",ex); } finally{ try{ if (pstmt!=null) pstmt.close(); }catch (Exception e){} try { if (conn!=null) conn.close(); }catch (Exception e){} } } public void ejbRemove() throws RemoveException{ System.out.println("ejbRemove() called");PreparedStatement pstmt=null; Connection conn=null; try{ conn=getConnection();pstmt=conn.prepareStatement("delete from orders where order_id=?"); pstmt.setInt(1,order_id.intValue());if (pstmt.executeUpdate()==0){ throw new RemoveException("failed to operate the database"); } }catch(Exception ex){ throw new EJBException(ex.toString()); } finally{ try{ if (pstmt!=null) pstmt.close(); }catch (Exception e){} try { if (conn!=null) conn.close(); }catch (Exception e){} } }
OrdersPK class extends java.io.*;序列化
public OrdersPK ejbCreate(java.lang.Integer order_id,java.lang.Integer product_no,java.lang.Integer quantity) throws CreateException{PreparedStatement pstmt=null;
Connection conn=null;
try{
conn=getConnection();
pstmt=conn.prepareStatement("insert into orders (order_id,product_no,quantity) values(?,?,?)");this.order_id=order_id;this.product_no=product_no;this.quantity=quantity;pstmt.setInt(1,order_id.intValue());pstmt.setInt(2,product_no.intValue());pstmt.setInt(3,quantity.intValue());pstmt.executeUpdate();return new OrdersPK(order_id);}catch (SQLException sqle){
throw new EJBException(sqle);
}finally{
try{
if (pstmt!=null)
{
pstmt.close();
}
if (conn!=null)
{
conn.close();
}
}catch(SQLException sqle){}
}
}public OrdersPK ejbFindByPrimaryKey(OrdersPK key) throws FinderException{
System.out.println("ejbFindByPrimaryKey() called");
PreparedStatement pstmt=null;
Connection conn=null;
try{
conn=getConnection();pstmt=conn.prepareStatement("select order_id from orders where order_id=?");
pstmt.setInt(1,key.order_id.intValue());ResultSet rs=pstmt.executeQuery();
if (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 void ejbLoad(){
System.out.println("ejbLoad() called");
OrdersPK key=(OrdersPK)ctx.getPrimaryKey();
System.out.println("key is"+key.toString());
java.lang.Integer order_id=key.order_id;
PreparedStatement pstmt=null;
Connection conn=null;
try{
conn=getConnection();pstmt=conn.prepareStatement("select order_id,product_no,quantity from orders where order_id=?");
pstmt.setInt(1,key.order_id.intValue());
ResultSet rs=pstmt.executeQuery();
rs.next();
order_id=rs.getString("order_id");
product_no=new Integer(rs.getInt("product_no"));
System.out.println("**load:product_no is"+product_no.toString());
quantity=new Integer(rs.getInt("quantity"));
}catch(Exception ex){
throw new EJBException("failed to load from the database"+ex);
}finally{System.out.println("order_id is"+order_id.toString());
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 orders set product_no=?,quantity=? where order_id=?");
pstmt.setInt(1,product_no.intValue());
pstmt.setInt(2,quantity.intValue());
pstmt.setInt(3,order_id.intValue());
pstmt.executeUpdate();
}catch(Exception ex){
throw new EJBException("failed to save to the database",ex);
}
finally{
try{
if (pstmt!=null) pstmt.close();
}catch (Exception e){}
try {
if (conn!=null) conn.close();
}catch (Exception e){}
}
}
public void ejbRemove() throws RemoveException{
System.out.println("ejbRemove() called");PreparedStatement pstmt=null;
Connection conn=null;
try{
conn=getConnection();pstmt=conn.prepareStatement("delete from orders where order_id=?");
pstmt.setInt(1,order_id.intValue());if (pstmt.executeUpdate()==0){
throw new RemoveException("failed to operate the database");
}
}catch(Exception ex){
throw new EJBException(ex.toString());
}
finally{
try{
if (pstmt!=null) pstmt.close();
}catch (Exception e){}
try {
if (conn!=null) conn.close();
}catch (Exception e){}
}
}