Question

Hi I am trying to fetch data from 2 place. One from Database and second from class file. After getting data I want to return those both ResultSet object to other file using return keyword but how to return 2 resultsets I don't know.

My code :

public ResultSet getdata(String query, boolean test, List<CartEntry> items)
    {
        int k;
            try
            {
                Connection conn;
                Statement st;
                public ResultSet rs,result;
                List<Product> prodList = new ArrayList<Product>();
                for(CartEntry ce : items)
                {
                    prodList.add(new Product("p" + ce.getpId(), "test", "prod" + ce.getpId(), (int)ce.getpId(), ce.getpId() + 0.12f, ce.getQuantity()));
                    result = new DummyResultSet(prodList);
                    k = ce.getQuantity();
                    System.out.println(k);
                }

                st=conn.createStatement();
                rs=st.executeQuery(query);          
                querystatus=true;
                return rs;
            }
            catch(Exception e)
            {
                querystatus=false;
            }
        }

the last line in try block i.e. return rs; I want something like return rs,result; but how to implement it I don't know. I have tried it simply like that but it doesn't work, it gives me an error. Anyone have Idea that how to return data to other file and how to retrieve it ??

Was it helpful?

Solution 3

Make changes in your code:

public ArrayList getdata(String query, boolean test, List<CartEntry> items)
    {
ArrayList arr = new ArrayList();// This is a ArrayList
        int k;
            try
            {
                Connection conn;
                Statement st;
                public ResultSet rs,result;
                List<Product> prodList = new ArrayList<Product>();
                for(CartEntry ce : items)
                {
                    prodList.add(new Product("p" + ce.getpId(), "test", "prod" + ce.getpId(), (int)ce.getpId(), ce.getpId() + 0.12f, ce.getQuantity()));
                    result = new DummyResultSet(prodList);
                    k = ce.getQuantity();
                    System.out.println(k);
                }

                st=conn.createStatement();
                rs=st.executeQuery(query);          
                querystatus=true;
arr.add(rs);// Add it to list
arr.add(result);
                return arr; // Return the List
            }
            catch(Exception e)
            {
                querystatus=false;
            }
        }

on receiving function do this:-

 ArrayList arr = getdata(query, test,items);


        ResultSet rs = (ResultSet)arr.get(0);
        ResultSet result = (ResultSet)arr.get(1);

OTHER TIPS

Other people have told you how to do it, so I'm adding my answer that explains that you should not do this.

You shouldn't basically return result sets, they're meant for consuming the answer: instead you should read up the result set, store the results somewhere (for example an array of value objects) and free the result. Otherwise you're easily leaking your database connection.

First thing very bad practice to return ResultSet from one layer to another layer as it doesn't implements Serializable interface.

Another bad thing is we are revealing our technology to Service layer that we are using JDBC in Data Access Layer where sometimes Service layer development can be done by some third party. We should not reveal to any other layer that what we are using viz JDBC or Hibernate or Spring JDBC.

So you can collect your data from ResultSet and store into a List and then return that List.

You can use ResultSet array or ResultSet List to return more than one ResultSet. Take a look following example which used ResultSet[].

public ResultSet[] getdata(String query, boolean test, List<CartEntry> items)
{
    int k;
        try
        {
            Connection conn;
            Statement st;
            ResultSet[] rs=new ResultSet[2];
            List<Product> prodList = new ArrayList<Product>();
            for(CartEntry ce : items)
            {
                prodList.add(new Product("p" + ce.getpId(), "test", "prod" + ce.getpId(), (int)ce.getpId(), ce.getpId() + 0.12f, ce.getQuantity()));
                rs[1] = new DummyResultSet(prodList);
                k = ce.getQuantity();
                System.out.println(k);
            }

            st=conn.createStatement();
            rs[0]=st.executeQuery(query);          
            querystatus=true;

        }
        catch(Exception e)
        {
            querystatus=false;
        }
       return rs;
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top