Im sorry if this has been asked before, but I i did use the search function. I have to construct an ArrayList, that holds all Borger (-> Read as Citizen) objects, but I'm pretty sure the way I am doing it so far is way off?
I need the ArrayList to insert the information(Citizen last and first name) into the JList on the right, as shown in picture.
I see a few problems here. First, where is your result set coming from. I see something that might open a database connection, but no query. Also, your for-loop loops up to the size of liste, but then adds objects to liste, which will increase the size. You probably mean to loop up to the number of columns. You might want to set that as a constant.
Joined: May 07, 2012
Is that any better in any sense, as in the ResultSet thing? About the for-loop, I figured that was wrong, it still is, though having a hard time figuring out how its done.
You probably mean to loop up to the number of columns.
Do you mean how many instances of the class Citizen there are in the Database, then yes.
You're still pretty far from a workable program. Among the problems I see are:
The open connection method on line 24 can't assign anything to the private variable con.
Con shouldn't be static. That's asking for trouble.
A typical select statement should have a "FROM table" part, which yours lacks.
Looping through the result set (while loop) gives you the Borgers one by one. You can think of it as looping through the rows in the table. The inner loop if you even need it would loop through the columns, ie., first name and last name, for one row and assign values into a Borger instance in your Java program.
If you are still at that stage, you ought to work directly on the database using the command line or a terminal. You write SELECT ??? FROM ??? WHERE ??? and you get a display on screen. When you have reliably worked out how to do that, you can consider using a connection to your Java™ code. You will find more in the tutorial Greg showed you, it is good.
Joined: May 07, 2012
I ended up getting help from a guy in my class(Also i found out i didnt need the other ArrayList for anything, though still needed help with Data -> ArrayList -> JList), and with him ended with this result, but yes I am aware of the fact that I should read some more about JDBC, and this i will do. Never the less, thanks for the help.