Help with result set

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • peterkay
    New Member
    • Apr 2007
    • 4

    #1

    Help with result set

    Can anybody help me with this? I have bean which connects to a db and runs a queary, i am then trying to go through the result set and add each item returned to a array list, at the moment i can add items to a array list by hard coding them in but am having trouble doing it with a loop:
    Code:
    public List getVideo(String p1, String p2) {
        ArrayList videos = new ArrayList();
        videoBean v1 = new videoBean();
        v1.setArtist("some artist"); // hard code the stuff here
    v1.setTitle("the title");
        videos.add(v1);
    
        return videos;
    	}
    Using this i method i can hard code data in and return it to where it was called but i want to do this in the while loop of:
    Code:
                String selectSQL = "Select artist_name, title" + 
                "from VideoTable" + "where category = " + CatPassed +" and price <= " + PricePassed +";";
                Statement stmt0 = conn.createStatement();
                ResultSet rs0 = stmt0.executeQuery(selectSQL); 
              
    //List VideoList = new ArrayList(); // use this instead i think // works if i put it as list not array list
                while(rs0.next()) {
                    videoBean collection = new videoBean();
                    collection.setArtist(rs0.getString("artist_name"));
                    collection.setTitle(rs0.getString("title"));
    		System.out.println(collection.getArtist()); // testing in terminal  
                }
                stmt0.close();
    Can anybody help me?
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by peterkay
    Can anybody help me with this? I have bean which connects to a db and runs a queary, i am then trying to go through the result set and add each item returned to a array list, at the moment i can add items to a array list by hard coding them in but am having trouble doing it with a loop:
    Code:
    public List getVideo(String p1, String p2) {
    ArrayList videos = new ArrayList();
    videoBean v1 = new videoBean();
    v1.setArtist("some artist"); // hard code the stuff here
    v1.setTitle("the title");
    videos.add(v1);
     
    return videos;
    	}
    Using this i method i can hard code data in and return it to where it was called but i want to do this in the while loop of:
    Code:
    String selectSQL = "Select artist_name, title" + 
    "from VideoTable" + "where category = " + CatPassed +" and price <= " + PricePassed +";";
    Statement stmt0 = conn.createStatement();
    ResultSet rs0 = stmt0.executeQuery(selectSQL); 
     
    //List VideoList = new ArrayList(); // use this instead i think // works if i put it as list not array list
    while(rs0.next()) {
    videoBean collection = new videoBean();
    collection.setArtist(rs0.getString("artist_name"));
    collection.setTitle(rs0.getString("title"));
    		System.out.println(collection.getArtist()); // testing in terminal 
    }
    stmt0.close();
    Can anybody help me?
    I don't see how you are using the videoBean here.
    Do you want to add videoBean objects to the arraylist? You can do so with
    Code:
     ArrayList collection = new ArrayList(); 
    while(rs0.next()) {
    	  videoBean video = new videoBean();
    	  video.setArtist(rs0.getString("artist_name"));
    	  video.setTitle(rs0.getString("title"));
    	  collection.add(video);
    }

    Comment

    • peterkay
      New Member
      • Apr 2007
      • 4

      #3
      Thanks i tried that but i just get a empty list returned, in the getvideo method what should i have in it now then?

      Code:
      public List getVideo(String p1, String p2) {
      ArrayList videos = new ArrayList();
      videoBean v1 = new videoBean();
      v1.setArtist("some artist"); // hard code the stuff here
      v1.setTitle("the title");
      videos.add(v1);
       
      return videos;
      	}
      I don't need any of the set bits in there but if i have a new array list i'm guessing thats why i get nothing returned.

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by peterkay
        Thanks i tried that but i just get a empty list returned, in the getvideo method what should i have in it now then?

        Code:
        public List getVideo(String p1, String p2) {
        ArrayList videos = new ArrayList();
        videoBean v1 = new videoBean();
        v1.setArtist("some artist"); // hard code the stuff here
        v1.setTitle("the title");
        videos.add(v1);
         
        return videos;
        	}
        I don't need any of the set bits in there but if i have a new array list i'm guessing thats why i get nothing returned.
        Have you stored anything in the database?

        Comment

        Working...