aspose file tools*
The moose likes Beginning Java and the fly likes Data out of MySQL into ArrayList and show in JList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Data out of MySQL into ArrayList and show in JList" Watch "Data out of MySQL into ArrayList and show in JList" New topic
Author

Data out of MySQL into ArrayList and show in JList

khaw hasnaj
Greenhorn

Joined: May 07, 2012
Posts: 6
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

Joined: Oct 01, 2001
Posts: 2851
    
  11

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

Joined: May 07, 2012
Posts: 6


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

Joined: Oct 01, 2001
Posts: 2851
    
  11

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

    Joined: Oct 13, 2005
    Posts: 38873
        
      23
    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

    Joined: May 07, 2012
    Posts: 6


    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.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Data out of MySQL into ArrayList and show in JList