如何从java中的Web服务返回结果集

发布于 2024-09-19 19:38:35 字数 1964 浏览 2 评论 0原文

我正在编写一个应用程序,在其中创建网络服务。我正在创建一个操作(方法),它从结果集中的数据库表中检索数据库表值。因此,我们无法直接在 Web 服务中返回结果集值。我正在创建一个类来保存结果集中的值。我返回的是新创建的类的 object[],而不是结果集,如下所示:

 public HistoryInfoByUser[] get_HistoryInfoByUser(@WebParam(name = "email_Id")
  String email_Id) throws Exception{

        HistoryInfoByUser[] historyIn = null;
        if (conn != null) {
        CallableStatement cst = conn.prepareCall("{call sp_xxxx(?)}",ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
            cst.setString(1, email_Id);
            ResultSet resultSet = cst.executeQuery();
             int rowCount = resultSet.getRow();

           historyIn = new HistoryInfoByUser[rowCount];

          while (resultSet.next())
             {

                historyIn[rowCounter].setId(rowCounter);                               
           historyIn[rowCounter].setStartTime((java.util.Date)resultSet.getObject(1));
                historyIn[rowCounter].setType((String) resultSet.getObject(2));


             rowCounter++;
            }
        }
        return historyIn;
    }

但是在尝试访问 Web 服务客户端中的这些值时,它给出了 java.lang.NullPointerException。

这是我在 Web 服务客户端中用于访问结果集的代码 value :

    public void get_HistoryInfoByUser(String email_Id) 
{      
               service = new DBService();

               port = service.getDBPort();

    try {

        List<HistoryInfoByUser> historyIn = port.getHistoryInfoByUser(email_Id);
      Iterator iterator = historyIn.iterator();
            while (iterator.hasNext()){
              System.out.print(iterator.next()+" ");  
            }
      } catch (Exception_Exception ex) {
        Logger.getLogger(DataBaseWSHelper.class.getName()).log(Level.SEVERE, null, ex);
    }

 }

我尝试通过返回新创建的类的对象 (HistoryInfoByUser) 而不是 object[] (HistoryInfoByUser[] ) 来返回单行值。它适用于单个对象,但当我使用 object[] 时给出 NullPointerException。我没有找到任何方法可以帮助我克服访问结果集值的问题。

我预先感谢您提出的所有宝贵建议,这些建议将帮助我克服这个问题。

i am writing an application in which i am creating web services. i am creating an operation(method) which retrieves database table values from database table in resultset. Hence we can't return resultset value directly in web services. i am creating a class which holds the values from resultset. instead of resultset i am returning object[] of newly created class as follows :

 public HistoryInfoByUser[] get_HistoryInfoByUser(@WebParam(name = "email_Id")
  String email_Id) throws Exception{

        HistoryInfoByUser[] historyIn = null;
        if (conn != null) {
        CallableStatement cst = conn.prepareCall("{call sp_xxxx(?)}",ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
            cst.setString(1, email_Id);
            ResultSet resultSet = cst.executeQuery();
             int rowCount = resultSet.getRow();

           historyIn = new HistoryInfoByUser[rowCount];

          while (resultSet.next())
             {

                historyIn[rowCounter].setId(rowCounter);                               
           historyIn[rowCounter].setStartTime((java.util.Date)resultSet.getObject(1));
                historyIn[rowCounter].setType((String) resultSet.getObject(2));


             rowCounter++;
            }
        }
        return historyIn;
    }

but while trying to access those values in web service client, it is giving java.lang.NullPointerException.

here is the code that i am using in web service client for accessing resultset
values :

    public void get_HistoryInfoByUser(String email_Id) 
{      
               service = new DBService();

               port = service.getDBPort();

    try {

        List<HistoryInfoByUser> historyIn = port.getHistoryInfoByUser(email_Id);
      Iterator iterator = historyIn.iterator();
            while (iterator.hasNext()){
              System.out.print(iterator.next()+" ");  
            }
      } catch (Exception_Exception ex) {
        Logger.getLogger(DataBaseWSHelper.class.getName()).log(Level.SEVERE, null, ex);
    }

 }

I tried returning a singe row value by returning an object (HistoryInfoByUser) of newly created class instead of object[] (HistoryInfoByUser[] ). It works fine with single object but giving NullPointerException when i am using object[]. i am not getting any way which will help me to overcome with this problem of accessing resultset value.

I thank in advance to your all valuable suggestions which will help me to overcome with this problem.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

紫竹語嫣☆ 2024-09-26 19:38:35

从您的网站返回 WebRowSet服务:

import javax.sql.rowset.WebRowSet;
import com.sun.rowset.WebRowSetImpl; // Reference implementation
...
// get ResultSet rs from db
WebRowSet wrs = new WebRowSetImpl();
wrs.populate(rs);
// return wrs from web service

请注意,您的 JDBC 提供程序可能提供特定于供应商的 WebRowSet 实现。您可以使用它来代替参考实现。

Return a WebRowSet from your web service:

import javax.sql.rowset.WebRowSet;
import com.sun.rowset.WebRowSetImpl; // Reference implementation
...
// get ResultSet rs from db
WebRowSet wrs = new WebRowSetImpl();
wrs.populate(rs);
// return wrs from web service

Note that your JDBC provider might provide a vendor-specific WebRowSet implementation. You can use that instead of the reference implementation.

动听の歌 2024-09-26 19:38:35

缺少一个

historyIn[rowCounter] = new HistoryInfoByUser();

您的 while 循环顶部 。创建数组时,您只会得到一个空指针数组。因此,在访问数组项之前,您需要创建它。

但如果你真的想做的话, gpeche 的答案可能是一个更好的主意。

You are missing a

historyIn[rowCounter] = new HistoryInfoByUser();

at the top of your while loop. When you create the array, you only get an array of null pointers. So before you can access an array item, you need to create it.

But gpeche's answer might be a better idea, if that is what you really want to do.

厌倦 2024-09-26 19:38:35

您不应该从 Web 服务或任何其他 Java 对象返回 ResultSet。

将数据加载到对象或数据结构中,并在创建它的同一范围内关闭 ResultSet。

充其量,您的设计将导致持久层泄漏;最坏的情况是,您将拥有未关闭的游标。

You should not be returning a ResultSet from a web service or any other Java object.

Load the data into an object or data structure and close the ResultSet in the same scope in which it was created.

At best, your design will have the persistence tier leaking out; at worst, you'll have unclosed cursors.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文