📄 commentcontrollerbean.java
字号:
package bookstore.ejb;import bookstore.util.*;import java.sql.*;import javax.sql.*;import java.util.*;import javax.ejb.*;public class CommentControllerBean implements SessionBean{ SessionContext sessionContext; private String commentID; private CommentHome commentHome; private Comment comment; private Connection con; /*************************** removal methods*****************************/public String createComment(CommentDetails commentValue) throws Exception { // makes a new comment and enters it into db, // returns commentId System.out.println("commentControllerBean createcomment"); try { comment = commentHome.create(commentValue); } catch (Exception ex) { throw new EJBException ("createcomment: " + ex.getMessage()); } return commentID; }// createcomment public void removeComment(String commentId) throws NoSuchEntityException { //抛出RunTimeException的子类,有利于事务管理 // removes comment from db System.out.println("commentControllerBean removecomment"); if (isCommentExist(commentId) == false) throw new NoSuchEntityException(commentId); try { comment.remove();//comment已在iscommentExist中获得 }catch (Exception ex) { throw new EJBException ("removecomment: " + ex.getMessage()); } } // removecomment public void updateComment(CommentDetails commentValue) throws NoSuchEntityException { System.out.println("commentControllerBean updatecomment"); if (isCommentExist(commentValue.getCommentID()) == false) throw new NoSuchEntityException(commentValue.getCommentID()); comment.setCustomerID(commentValue.getCustomerID()); comment.setBookID(commentValue.getBookID()); comment.setRank(commentValue.getRank()); comment.setContent(commentValue.getContent()); }//updatecomment public CommentDetails getCommentValue(String commentId) { System.out.println("commentControllerBean getcommentValue"); try { comment = commentHome.findByPrimaryKey(commentId); this.commentID = commentId; } catch (Exception ex) { return null; } return comment.getDetails(); }/******************************ejb methods*********************************/ public void ejbCreate() throws CreateException { System.out.println("commentControllerBean ejbCreate"); try { //创建下一层的引导接口 commentHome=EJBGetter.getCommentHome(); } catch (Exception ex) { System.out.println("commentControllerBean catch"); throw new EJBException("ejbCreate: " + ex.getMessage()); } comment = null; commentID = null; }//ejbCreate public void ejbRemove() { /**@todo Complete this method*/ } public void ejbActivate() { /**@todo Complete this method*/ } public void ejbPassivate() { /**@todo Complete this method*/ } public void setSessionContext(SessionContext sessionContext) { this.sessionContext = sessionContext; } /**********************************util methods****************************/ private boolean isCommentExist(String commentId) { // If a business method has been invoked with // a different commentId, then update the // commentId and comment variables. // Return null if the comment is not found. System.out.println("commentControllerBean iscommentExist"); if (commentId.equals(this.commentID) == false) { try { comment = commentHome.findByPrimaryKey(commentId); this.commentID = commentId; } catch (Exception ex) { return false; } } // if return true; }// iscommentExist}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -