Results 1 to 12 of 12
- 08-08-2011, 01:08 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 33
- Rep Power
- 0
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
"GUI" ClassJava 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; }
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; }
- 08-08-2011, 02:40 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
Presumably that should be returning a List<Branch> then?Java Code:public Branch getAllBranches(Connection con)
- 08-08-2011, 02:55 PM #3
Member
- Join Date
- Oct 2010
- Posts
- 33
- Rep Power
- 0
- 08-08-2011, 02:59 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
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.
- 08-08-2011, 03:03 PM #5
Member
- Join Date
- Oct 2010
- Posts
- 33
- Rep Power
- 0
see I already did that and it doesn't return anything
- 08-08-2011, 03:10 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
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.
- 08-08-2011, 03:12 PM #7
Member
- Join Date
- Oct 2010
- Posts
- 33
- Rep Power
- 0
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;
- 08-08-2011, 03:29 PM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 08-08-2011, 03:41 PM #9
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
Also (though not the problem:
should be:Java Code:l1.add(br = new Branch(rs.getInt(1), rs.getString(2)));
as the assignment to br is completely redundant.Java Code:l1.add(new Branch(rs.getInt(1), rs.getString(2)));
- 08-08-2011, 03:45 PM #10
Member
- Join Date
- Oct 2010
- Posts
- 33
- Rep Power
- 0
- 08-08-2011, 03:59 PM #11
Member
- Join Date
- Oct 2010
- Posts
- 33
- Rep Power
- 0
Solved the problem, thank you people
Mapper Class
GUI ClassJava 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; }
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; }
- 08-08-2011, 04:01 PM #12
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
To quote the API:Java Code:jComboBox.addItem(br);
"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
-
Objects and jCombobox
By norbertK in forum AWT / SwingReplies: 7Last Post: 07-20-2011, 02:29 AM -
Save/Load ArrayList
By chielt in forum New To JavaReplies: 3Last Post: 05-08-2011, 06:12 PM -
Jcombobox ArrayList
By Vith in forum New To JavaReplies: 1Last Post: 12-12-2010, 03:11 PM -
Arraylist Save and Load
By frankycool in forum Advanced JavaReplies: 1Last Post: 11-14-2009, 10:29 PM -
How can i store ArrayList objects in Access database
By frankycool in forum Advanced JavaReplies: 3Last Post: 11-04-2009, 06:55 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks