|
|
oracle's package P_T_Reoprt
create or replace package P_T_Reoprt as type v_cursor is ref cursor; function T_Reoprt (j varchar2) return v_cursor; end P_T_Reoprt;
create or replace package body P_T_Reoprt as function T_Reoprt (j varchar2) return v_cursor is rc v_cursor; begin open rc for select * from customer where customer.sex = j; return rc; end; end P_T_Reoprt;
------------------------------------------------
DBConnection.java(a method of the javabean's source) .... public ResultSet executeProduce(){ rs = null; try { //connect the database conn = DriverManager.getConnection("jdbc racle:thin:@192.168.0.176:1521:yzttest","bill","bill" ; System.out.println("004:connection success!" ; // Prepare a PL/SQL call CallableStatement call = conn.prepareCall ("{?=call P_T_Reoprt.T_Reoprt(?)}" ; System.out.println("005:prepare PL/SQL Call success!" ; // Find out all Record call.registerOutParameter (1,OracleTypes.CURSOR); call.setString (2,"M" ; System.out.println("006 L/SQL define success !" ; call.execute (); System.err.println("007 L/SQL execute success!" ; ResultSet rs = (ResultSet)call.getObject (1); System.out.println("008:take resultset success!" ; } catch(SQLException ex) { System.err.println("012:fail to DBConnection.executeProduce: " + ex.getMessage()); } return rs; } ....
------------------------------------------------
when i invok that DBConnection's executeProduce method in the Servlet, that had happened and error when execute at "while(rs.next())" ... DBConnection JavaBean = new DBConnection(); System.out.println("001:JavaBean instance success!" ; ResultSet rs=JavaBean.executeProduce(); System.out.println("002:method invok success!" ; while (rs.next())//<<--------------that has error System.out.println("018:there have record!" ; System.out.println (rs.getString(1)); System.out.println("013:return record success!" ; ...
What way is right to invok ResultSet form JavaBean in Servlet? My way is error,now?
| cinc 回复于:2002-10-30 00:12:56
| [这个贴子最后由cinc在 2002/10/30 00:16am 编辑]
You can use DAO Pattern and MVC Framework:
Servlet, JSP -> JavaBean and DAO -> Database
Servlet or JSP is the View layer of MVC Framework. It must not contain any codes that directly access database.
JavaBean and DAO is the Model layer of MVC Framework. JavaBeans is the representation of table in database You can place business code in DAO Object, including code communicating with database.
For example:
If you have a table in database called : article
create table article( id integer, title char(10), content char(30) )
1.Model layer : JavaBean and DAO Object
You can create a JavaBean to represent an article in database
public class Article{ int id; String title; String content; public void set...(){ } public ... get...(){ } ... }
we ususally use DAO Object to access database, for article, we create:
public class ArticleDAO{ /** * find one article, return an article */ public Article findById(int id){ // execute SQL : select title, content from article where id = 10; Article article = new Article(); article.setId(id); article.setTitle(title); article.setContent(content); return article; } /** * find all the articles which contains the keyword, return a vector * containing these articles */ public Vector findByKeyword(String keyword){ Vector resultVector = new Vector(); // execute SQL : select title, content from article where content like "%keyword%"; while (resultset.next){ Article article = new Article(); article.setId(id); article.setTitle(title); article.setContent(content); resultVector.add (article); } return resultVector; } }
2.View Layer : servlet or jsp /** * user want to find all the article that contain the keyword */ public void doGet(...){ String keyword = getParameter("keyword" ; ArticleDAO articleDAO = new ArticleDAO(); Vector resultVector = articleDAO.findByKeyword(keyword); // display each article in resultVector }
DAO Pattern is a widely used pattern in web development of servlet and jsp more detail: http://java.sun.com/blueprints/patterns/DAO.html
Other consideration about design pattern: use Factory to Create DAO Object public class DAOFactory{ public ArticleDAO getArticleDAO(){ ... } ... } Factory is an Singleton
:)
| | bill1 回复于:2002-10-30 09:36:38
| No, my operation bean encapsuled in PL/SQL.
| | cinc 回复于:2002-10-30 10:40:50
| 呵呵,没做过,不过应该是一样的。 你先写个 application (带 main 的)试试。
:)
| | pilgram 回复于:2002-11-02 11:44:03
| </pre> because the v_cursor is not initialized ,it's an invalidate cursor please refer to "http://www.csee.umbc.edu/help/oracle8/java.815/a64685/samapp3.htm" you can change it as following:
oracle's package P_T_Reoprt create or replace package P_T_Reoprt type v_cursor is ref cursor return table_name%ROWTYPE; function T_Reoprt (j varchar2) return v_cursor; end P_T_Reoprt;
| |
|