DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 5 of 5
  1. #1
    Join Date
    Jan 2005
    Posts
    5

    working with multiple records using sql

    Hello all,
    I have created my first value objects; passed a search variable to my data access class, ran the query, populated the value object and returned it. Pretty straightforward.

    Now I need to learn how to do the same with multiple records. I thought to populate an arraylist with a value object for each record. But when I try to retrieve one of the elements:
    "ValueObject = ArrayList.get(i)"
    I get an error saying it can't convert an Object to my ValuObject
    Is there another method I need to use to retrieve the object I save to an arraylist so that I can then access that object's methods?

    In the test method below I am trying to populate the arraylist and then return 1 of the valueobject elements.


    private RequestVO selQry(String query,RequestVO cust)
    {//populates the Value Object with the Query string
    ArrayList ranklist = new ArrayList();
    try
    {
    java.sql.Statement stmt = Con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);

    ResultSet rsName = stmt.executeQuery(query);
    while(rsName.next())
    {
    cust.setReqID(rsName.getInt("RequestID"));
    cust.setReqTitle(rsName.getString("RequestTitle"));
    ranklist.add(i, cust);
    i++;
    }
    }
    catch(Exception e)
    { System.out.println(e.getMessage());
    }
    cust = ranklist.get(2);
    return cust;
    }

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    You must typecast the Object pointer returned from the ArrayList in order to make the java compiler understand what you are doing.

    in the line:

    cust = ranklist.get(2);

    you use the typechast syntax to do this:

    cust = (RequestVO) ranklist.get(2);
    eschew obfuscation

  3. #3
    Join Date
    Jan 2005
    Posts
    5
    Originally posted by sjalle
    You must typecast the Object pointer returned from the ArrayList in order to make the java compiler understand what you are doing.

    in the line:

    cust = ranklist.get(2);

    you use the typechast syntax to do this:

    cust = (RequestVO) ranklist.get(2);
    Thanks very much!
    I'm entering it wrong somehow though, because I keep displaying only the last record added. (which admittedly is an improvement over what I'd had before )

    I've tried a couple of different things with no change. Here's what I've entered:

    cust = (RequestVO) ranklist.get(2);
    System.out.println(cust.getReqID());

    RequestVO cust2 = new RequestVO();
    cust2 = (RequestVO) ranklist.get(1);
    System.out.println("cust2 = "+cust2.getReqID());

    RequestVO cust3 = (RequestVO)ranklist.get(2);
    System.out.println("cust3 = "+cust3.getReqTitle());
    return cust;

  4. #4
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    To make a long story short, I've coded these two snippets:

    First, I've altered your request method to what I think you want it to do....

    Code:
    private ArrayList selQry(String query) {
        //populates the Value Objects with the Query string
        ArrayList ranklist = new ArrayList();
        try {
          java.sql.Statement stmt = con.createStatement(ResultSet.
              TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
          ResultSet rsName = stmt.executeQuery(query);
          while (rsName.next()) {
            // ** You must instantiate a new object for each record:
            RequestVO cust=new RequestVO();
            cust.setReqID(rsName.getInt("RequestID"));
            cust.setReqTitle(rsName.getString("RequestTitle"));
            ranklist.add(cust);
          }
        }
        catch (Exception e) {
          System.out.println(e.getMessage());
        }
        return ranklist; //** return the whole lot
      }
    And this is how I think you want to use it:

    Code:
      ArrayList aList=selQry("select * from atable where RequestID > 10");
      for (int i=0;i<aList.size();i++) {
          RequestVO rvo=(RequestVO)aList.get(i);
          System.out.println("reqTitle: "+rvo.getReqTitle());
          System.out.println("regID: "+rvo.getReqID());
      }
    eschew obfuscation

  5. #5
    Join Date
    Jan 2005
    Posts
    5
    You Rock.

    Thanks very much,
    Chris

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links