循速渐进学用Session Bean(五) - 编程入门网
作者 佚名技术
来源 NET编程
浏览
发布时间 2012-06-25
("Unable to access database: "+exc.toString());}}/** Called by the EJB container to tell this session bean that it is beingsuspended from use (it''s being put to sleep). */public void ejbPassivate()throws EJBException{try{// Shut down the current database connectionconn.close();conn = null;}catch (Exception exc){throw new EJBException("Unable to close database connection: "+exc.toString());}}/** Called by the EJB container to wake this session bean up after ithas been put to sleep with the ejbPassivate method. */public void ejbActivate()throws EJBException{try{// When the bean wakes back up, get a database connection againconn = getConnection();}catch (Exception exc){throw new EJBException("Unable to access database: "+exc.toString());}}/** Called by the EJB container to tell this session bean that it has beenremoved, either because the client invoked the remove() method or thecontainer has timed the session out. */public void ejbRemove()throws EJBException{try{// Shut down the current database connectionconn.close();conn = null;}catch (Exception exc){throw new EJBException("Unable to close database connection: "+exc.toString());}}/** Returns a list of the available product codes */public String[] getProductCodes()throws EJBException{Statement s = null;try{s = conn.createStatement();ResultSet results = s.executeQuery("select product_code from price");Vector v = new Vector();// Copy the results into a temporary vectorwhile (results.next()){v.addElement(results.getString("product_code"));}// Copy the vector into a string arrayString[] productCodes = new String[v.size()];v.copyInto(productCodes);return productCodes;}catch (Exception exc){throw new EJBException("Unable to get product codes: "+exc.toString());}finally{// Close down the statement in a finally block to guarantee that it gets// closed, whether an exception occurred or nottry{s.close();}catch (Exception ignore){}}}/** Gets the price for a particular product code */public double getPrice(String productCode)throws EJBException, InvalidProductCodeException{PreparedStatement ps = null;try{// It''s always better to use a prepared statement than to try to insert// a string directly into the query string. This way you don''t have to// worry if there''s a quote in the product codeps = conn.prepareStatement("select price from price where product_code = ?");// Store the product code in the prepared statementps.setString(1, productCode);ResultSet results = ps.executeQuery();// If there are any results, get the first one (there should only be one)if (results.next()){return results.getDouble("price");}else{// Otherwise, if there were no results, this product code doesn''t existthrow new InvalidProductCodeException(productCode);}}catch (SQLException exc){throw new EJBException("Unable to get price: "+exc.toString());}finally{// Close down the statement in a finally block to guarantee that it gets// close |
凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站:http://www.lingzhong.cn 为了给广大客户了解更多的技术信息,本技术文章收集来源于网络,凌众科技尊重文章作者的版权,如果有涉及你的版权有必要删除你的文章,请和我们联系。以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢! |
你可能对下面的文章感兴趣
关于循速渐进学用Session Bean(五) - 编程入门网的所有评论