Results 1 to 12 of 12
  1. #1
    jeata is offline Member
    Join Date
    Oct 2010
    Posts
    33
    Rep Power
    0

    Default Load objects from DataBase into an ArrayList, then into a jComboBox in a GUI

    Hello

    I'm having a small problem adding objects that I fetched from my database to an arraylist. I'm using 3 classes(different pacakges). A class called "BranchMapper.java"(the mapper from the db obviously), "Branch.java" (The object class) and "BranchGUI"(where the jComboBox resides). I've did this so far. I need the objects to be added to an ArrayList in the mapper class, so I can fetch the arraylist and append it to the jComboBox in the visual class

    BranchMapper class

    Java Code:
    public Branch getAllBranches(Connection con) 
        {
    		Branch br = null;
    		String SQLString1 =       // get branch
                "select * from branch";
           PreparedStatement statement=null;
            try
            {
                //=== get branch
                statement = con.prepareStatement(SQLString1);
                ResultSet rs  = statement.executeQuery();
                if(rs.next())
                {
                	
                      br = new Branch(rs.getInt(1), 
                                      rs.getString(2));
                }                     
            }
            catch (Exception exc)
            {   
                System.out.println("Fail in BranchMapper - getAllBranches");
                System.out.println(exc.getMessage());
            }
            
            return br;                         
        }
    	
    	
         public ArrayList<Branch> branchArray(){
    		return null;
        	 
         }

    "Branch" Class

    Java Code:
    int branchid;
    	  String branchname;
    	  
    	  public Branch(int bi, String bn)
    	  {
    	    branchid=bi;
    	    branchname=bn;
    	       
    	  }
    
    	  //======AUTOGENERATED GETTERS AND SETTERS======//
    	  
    	   public int getBranchid() {
    			return branchid;
    		}
    
    		public void setBranchid(int branchid) {
    			this.branchid = branchid;
    		}
    
    		public String getBranchname() {
    			return branchname;
    		}
    
    		public void setBranchname(String branchname) {
    			this.branchname = branchname;
    		}
    "GUI" Class

    Java Code:
    private JComboBox getJComboBox() {
    		if (jComboBox == null) {
    			jComboBox = new JComboBox();
    			jComboBox.setBounds(new Rectangle(3, 80, 280, 24));
    			try{
    				BranchMapper bm = new BranchMapper();
    				Branch br = bm.getAllBranches(getConnection());
    				String brn = br.getBranchname();
    				String[] numStrings = {brn};
    
    	             for(int i=0; i<numStrings.length; i++)
    	               {
    		              jComboBox.addItem(new String(numStrings[i]));
    	               }
    			  }catch (Exception exasd)
    			  {
    				  System.out.println(exasd);
    			  }
    			  
    			  //Directly load into comboBox
    			  
    			  
    		}
    		return jComboBox;
    	}

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default

    Java Code:
    public Branch getAllBranches(Connection con)
    Presumably that should be returning a List<Branch> then?

  3. #3
    jeata is offline Member
    Join Date
    Oct 2010
    Posts
    33
    Rep Power
    0

    Default

    Yes, thats right. The main question is still the same: How do I make it iterate through the objects, adding them to the arraylist or list ??

    Quote Originally Posted by Tolls View Post
    Java Code:
    public Branch getAllBranches(Connection con)
    Presumably that should be returning a List<Branch> then?

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default

    You're already iterating over the result set, so just add each new Branch to a List.
    I really don't see what problem you're having because you have just described exactly what you need to do, and you even have code that almost does it.

    Return a List<Branch> object instead of the Branch you are currently returning and go from there.

  5. #5
    jeata is offline Member
    Join Date
    Oct 2010
    Posts
    33
    Rep Power
    0

    Default

    see I already did that and it doesn't return anything

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default

    So why didn't you show that code?
    Then we could explain why that code (which is nearer to what you need) is going wrong.

  7. #7
    jeata is offline Member
    Join Date
    Oct 2010
    Posts
    33
    Rep Power
    0

    Default

    Showing Code now

    Mapper Class
    Java Code:
    public ArrayList<Branch> getAllBranches(Connection con) 
        {
    		ArrayList l1 = null;
    		Branch br = null;
    		String SQLString1 =       // get branch
                "select * from branch";
           PreparedStatement statement=null;
            try
            {
                //=== get branch
                statement = con.prepareStatement(SQLString1);
                ResultSet rs  = statement.executeQuery();
                while(rs.next())
                {
                	 l1.add(br = new Branch(rs.getInt(1), rs.getString(2)));
                      
                }                     
            }
            catch (Exception exc)
            {   
                System.out.println("Fail in BranchMapper - getAllBranches");
                System.out.println(exc.getMessage());
            }
            
            return l1;

    GUI Class
    Java Code:
    private JComboBox getJComboBox() {
    		if (jComboBox == null) {
    			jComboBox = new JComboBox();
    			jComboBox.setBounds(new Rectangle(3, 80, 280, 24));
    			try{
    				BranchMapper bm = new BranchMapper();
    	            ArrayList br = bm.getAllBranches(getConnection());
    		        jComboBox.addItem(br);
    	         
    			  }catch (Exception exasd)
    			  {
    				  System.out.println(exasd);
    			  }
    			  
    			  //Directly load into comboBox
    			  
    			  
    		}
    		return jComboBox;

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,528
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by jeata View Post
    Showing Code now

    Mapper Class
    Java Code:
    public ArrayList<Branch> getAllBranches(Connection con) 
        {
    		ArrayList l1 = null;
    		Branch br = null;
    		String SQLString1 =       // get branch
                "select * from branch";
           PreparedStatement statement=null;
            try
            {
                //=== get branch
                statement = con.prepareStatement(SQLString1);
                ResultSet rs  = statement.executeQuery();
                while(rs.next())
                {
                	 l1.add(br = new Branch(rs.getInt(1), rs.getString(2)));
                      
                }
    Your ArrayList<Branch> l1 is null (see your own code) and you never gave it a non-null value before you're trying to add values to it (also see your own code). That is a nono.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default

    Also (though not the problem:
    Java Code:
                	 l1.add(br = new Branch(rs.getInt(1), rs.getString(2)));
    should be:
    Java Code:
                	 l1.add(new Branch(rs.getInt(1), rs.getString(2)));
    as the assignment to br is completely redundant.

  10. #10
    jeata is offline Member
    Join Date
    Oct 2010
    Posts
    33
    Rep Power
    0

    Default

    Doesn't help, I gave the ArrayList an instantiation, but still doesn't solve the problem, gives only one result, not an ArrayList of results

    Java Code:
    ArrayList<Branch> l1 = new ArrayList<Branch>();
    Quote Originally Posted by JosAH View Post
    Your ArrayList<Branch> l1 is null (see your own code) and you never gave it a non-null value before you're trying to add values to it (also see your own code). That is a nono.

    kind regards,

    Jos

  11. #11
    jeata is offline Member
    Join Date
    Oct 2010
    Posts
    33
    Rep Power
    0

    Default

    Solved the problem, thank you people

    Mapper Class
    Java Code:
    public ArrayList<Branch> getAllBranches(Connection con) 
        {
    		ArrayList<Branch> l1 = new ArrayList<Branch>();
    		String SQLString1 =       // get branch
                "select * from branch";
           PreparedStatement statement=null;
            try
            {
                //=== get branch
                statement = con.prepareStatement(SQLString1);
                ResultSet rs  = statement.executeQuery();
                while(rs.next())
                { 
                    l1.add(new Branch(rs.getInt(1), rs.getString(2)));
                }                     
            }
            catch (Exception exc)
            {   
                System.out.println("Fail in BranchMapper - getAllBranches");
                System.out.println(exc);
            }
            
            return l1;                         
        }
    GUI Class
    Java Code:
    	private JComboBox getJComboBox() {
    		if (jComboBox == null) {
    			jComboBox = new JComboBox();
    			jComboBox.setBounds(new Rectangle(3, 80, 280, 24));
    			try{
    				BranchMapper bm = new BranchMapper();
    	            ArrayList<Branch> br = bm.getAllBranches(getConnection());
    	            Iterator<Branch> itr = br.iterator();
    	            while (itr.hasNext()) 
    	            {
    	              Branch branch = itr.next();
    	              jComboBox.addItem(branch.getBranchname());
    	              System.out.print("BranchName is");
    	            }
    	         
    			  }catch (Exception exasd)
    			  {
    				  System.out.println(exasd);
    			  }
    			  
    			  //Directly load into comboBox
    			  
    			  
    		}
    		return jComboBox;
    	}

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default

    Java Code:
    		        jComboBox.addItem(br);
    To quote the API:
    "Adds an item to the item list."

    Which in your case seems to be the array list, which will display the toString of that List.

Similar Threads

  1. Objects and jCombobox
    By norbertK in forum AWT / Swing
    Replies: 7
    Last Post: 07-20-2011, 02:29 AM
  2. Save/Load ArrayList
    By chielt in forum New To Java
    Replies: 3
    Last Post: 05-08-2011, 06:12 PM
  3. Jcombobox ArrayList
    By Vith in forum New To Java
    Replies: 1
    Last Post: 12-12-2010, 03:11 PM
  4. Arraylist Save and Load
    By frankycool in forum Advanced Java
    Replies: 1
    Last Post: 11-14-2009, 10:29 PM
  5. How can i store ArrayList objects in Access database
    By frankycool in forum Advanced Java
    Replies: 3
    Last Post: 11-04-2009, 06:55 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •