Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is this the correct way to get data in my Jframe ?

 
Chris Green
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Still a newbie when it comes to Java, therefor this question. I am trying to create a simple movie database program. I already created a MySQL database, with some tables. In 3 tables a already put in some data thanks to some queries:
- Table: media (Contains: VHS, DVD, BluRay)
- Table: genre (Contains: Action, Horror, History,....)
- Table: location(Contains different countrynames)


Next I created a Java Jframe with 3 jcomboboxes. I have already realised filling them with the data from the 3 tables above... HURAY :p
However, I have some doubts about if I am doing it in the correct WAY. This is how I did it
- Created another java class called DatabaseConnection with the following code:



Next in my Frame.java here is part of the code I used to populated the 3 JComboxes. My question is....is this the way that I need to do this...or is it sloppy work ?!




 
Knute Snortum
Bartender
Pie
Posts: 2903
62
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It works: that's a big step.  Here are my thoughts on the program.

  • I don't think you need the Class.forName line for modern JDBC drivers.  Try it without.
  • I don't like using the JOptionPane to display errors.  Look into some basic logging techniques.
  • In general, a variable should be declared as close to where it is used as possible, so instead of...


  • Just write...

     
    Chris Green
    Greenhorn
    Posts: 15
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Knute Snortum wrote:It works: that's a big step.  Here are my thoughts on the program.

  • I don't think you need the Class.forName line for modern JDBC drivers.  Try it without.
  • I don't like using the JOptionPane to display errors.  Look into some basic logging techniques.
  • In general, a variable should be declared as close to where it is used as possible, so instead of...


  • Just write...



    Thanks for the helpfull feedback. And what about the way I am writing the code itself. By that I mean:
    * Is it ok to Create 3 statements and 3 resultset ? Can't you reuse the same statement and resultset over and over again ? Or is it ok to create one per query you need ?
    * Is it ok to write all this code in the frame.class or do more experienced java developers use another class to write all there queries ?

     
    Dave Tolls
    Ranch Hand
    Posts: 2103
    15
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Two things leap out at me.
    First is extending JFrame.  There's almost never a reason to do this.  Are you actually adding any functionality to a JFrame, or are you simply populating one?  In every case I've seen it's the latter, so extending a JFrame is a bit like extending an ArrayList in order to populate it with data.  Of course, this is something you find in lots of examples on the internet so it's not surprising.

    The second thing is your data loading.
    I would suggest doing this in a worker thread (SwingWorker tutorial).
    So you launch the app, build the frame and display it, then launch the worker to grab the data.  In your case it may not take long to get the data, but with larger data sets, or remote databases, you could well lock up your gui while building the combobox data.
    This would also remove all the database access code from your gui, as you can then stick it in its own class.
     
    Chris Green
    Greenhorn
    Posts: 15
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Dave Tolls wrote:Two things leap out at me.
    First is extending JFrame.  There's almost never a reason to do this.  Are you actually adding any functionality to a JFrame, or are you simply populating one?  In every case I've seen it's the latter, so extending a JFrame is a bit like extending an ArrayList in order to populate it with data.  Of course, this is something you find in lots of examples on the internet so it's not surprising.

    The second thing is your data loading.
    I would suggest doing this in a worker thread (SwingWorker tutorial).
    So you launch the app, build the frame and display it, then launch the worker to grab the data.  In your case it may not take long to get the data, but with larger data sets, or remote databases, you could well lock up your gui while building the combobox data.
    This would also remove all the database access code from your gui, as you can then stick it in its own class.


    Extending JFrame is something that was automatically added by netbeans. I used netbeans Design view to create the JFrame. The code was automatically generated i guess... I will look in to the Swingworker functionality. Thanks for the tip!
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic