中国IT动力,最新最全的IT技术教程
最新100篇 | 推荐100篇 | 专题100篇 | 排行榜 | 搜索 | 在线API文档
首 页 | 程序开发 | 操作系统 | 软件应用 | 图形图象 | 网络应用 | 精文荟萃 | 教育认证 | 硬件维护 | 未整理篇 | 站长教程
ASP JS PHP工程 ASP.NET 网站建设 UML J2EESUN .NET VC VB VFP 网络维护 数据库 DB2 SQL2000 Oracle Mysql
服务器 Win2000 Office C DreamWeaver FireWorks Flash PhotoShop 上网宝典 CorelDraw 协议大全 网络安全 微软认证
硬件维护  CPU  主板  硬盘  内存  显卡  显示器  键盘鼠标  声卡音箱  打印机  机箱电源  BIOS  网卡  C#  Java  Delphi  vs.net2005
  当前位置:> 程序开发 > 编程语言 > Java > 综合文章
What way is right to invok ResultSet form JavaBean in Servle
作者:未知 时间:2005-09-13 19:35 出处:ChinaUnix.net 责编:chinaitpower
              摘要:What way is right to invok ResultSet form JavaBean in Servle

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("jdbcracle: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("006L/SQL define success !";
    call.execute ();
    System.err.println("007L/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;


 


关闭本页
 
首页 | 投资与合作 | 服务条款 | 隐私政策 | 收藏本站 | 设为首页 | 新用户注册 | 免责声明 | 使用帮助
Copyright ©2005-2008 chinaitpower.com All rights reserved. www.chinaitpower.com 版权所有