• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Data out of MySQL into ArrayList and show in JList

 
khaw hasnaj
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

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.





Any help is appreciated, thanks.
 
Greg Charles
Sheriff
Posts: 2985
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, and welcome to JavaRanch!

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.
 
khaw hasnaj
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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.

thanks
 
Greg Charles
Sheriff
Posts: 2985
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

    You'd benefit from working through a tutorial first. Try http://docs.oracle.com/javase/tutorial/jdbc/index.html, and check out our FAQ at http://www.coderanch.com/how-to/java/JdbcFaq
     
    Campbell Ritchie
    Sheriff
    Posts: 48652
    56
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    khaw hasnaj
    Greenhorn
    Posts: 6
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator


    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.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic