Results 1 to 10 of 10
  1. #1
    Kenjitsuka is offline Member
    Join Date
    Oct 2010
    Posts
    18
    Rep Power
    0

    Default Returning a ResultSet to custom JTable Model

    Hello everyone,

    I've been putting in a lot of hours on my hobby project for a friend, with some reasonable classes as result.
    But now comes the hard part I've been dreading; tying it all together.

    For instance, I have three classes that must return database results in a nice gui (JTable).

    The GUI class has a JTable in it, with a custom TableModel.
    I have a second class which exclusively makes the TableModel for me based on a resultset.
    The third class is one that gets the data from the database.

    My problem is, the database connection class gets a resultset (obviously), which I can read and print to the console (for testing purely).
    So the database connection and retrieval work, but it's a void class.
    Now I need it to return it's resultset to the TableModel class, which I can't seem to work.

    I've tried making it's main method a ResultSet instead of void, and added the line "return rs;" at the appropriate point.

    Then I get these errors:
    java.lang.NoSuchMethodError: main
    Exception in thread "main".

    I don't know what's wrong, the IDE also doesn't know.

    Your suggestions are greatly appreciated!

    Java Code:
    package laadpackage;
    
    import java.sql.*;
    
    public class dbGetter{
        public static void main(String[] args) {
    
        
            try {
                 //Class.forName("com.mysql.jdbc.Driver");
                Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/eurovip", "root", "admin");
                //Connection conn = null;
                Statement stmt = null;
                ResultSet rs = null;
                stmt = conn.createStatement();
                rs = stmt.executeQuery("SELECT invoerDatum, MRZ1, MRZ2, MRZ_TYPE, MRZ_ISSUE_COUNTRY, MRZ_NAME, MRZ_DOCUMENT_NUMBER, MRZ_NATIONALITY, MRZ_SEX, MRZ_BIRTH_DATE, MRZ_EXPIRY_DATE, MRZ_PERSONAL_DATA, TYPE, ISSUE_COUNTRY, NAME, SURNAME, GIVENNAME, DOCUMENT_NUMBER, NATIONALITY, SEX, BIRTH_DATE, EXPIRY_DATE, PERSONAL_DATA, MRZ_FIELDS, HOTEL, bestandsnaam FROM namen");
                int rijteller = 0;
                while (rs.next())
                {
                String invoerDatum = rs.getString("invoerDatum");
                String NAME = rs.getString("NAME");
                String SURNAME = rs.getString("SURNAME");
                String GIVENNAME = rs.getString("GIVENNAME");
                String DOCUMENT_NUMBER = rs.getString("DOCUMENT_NUMBER");
                String NATIONALITY = rs.getString("NATIONALITY");
                String SEX = rs.getString("SEX");
                String BIRTH_DATE = rs.getString("BIRTH_DATE");
                String EXPIRY_DATE = rs.getString("EXPIRY_DATE");
                String PERSONAL_DATA = rs.getString("PERSONAL_DATA");
                String TYPE = rs.getString("TYPE");
                String ISSUE_COUNTRY = rs.getString("ISSUE_COUNTRY");
                String MRZ1 = rs.getString("MRZ1");
                String MRZ2 = rs.getString("MRZ2");
                String MRZ_SEX = rs.getString("MRZ_SEX");
                String MRZ_FIELDS = rs.getString("MRZ_FIELDS");
                String MRZ_TYPE = rs.getString("MRZ_TYPE");
                String MRZ_ISSUE_COUNTRY = rs.getString("MRZ_ISSUE_COUNTRY");
                String MRZ_NAME = rs.getString("MRZ_NAME");
                String MRZ_DOCUMENT_NUMBER = rs.getString("MRZ_DOCUMENT_NUMBER");
                String MRZ_NATIONALITY = rs.getString("MRZ_NATIONALITY");
                String MRZ_BIRTH_DATE = rs.getString("MRZ_BIRTH_DATE");
                String MRZ_EXPIRY_DATE = rs.getString("MRZ_EXPIRY_DATE");
                String MRZ_PERSONAL_DATA = rs.getString("MRZ_PERSONAL_DATA");
                String HOTEL = rs.getString("HOTEL");
                String bestandsnaam = rs.getString("bestandsnaam");
                
                rijteller ++;
                System.out.println("Resultaat " + rijteller + " :" + invoerDatum + NAME + SURNAME + GIVENNAME + DOCUMENT_NUMBER + NATIONALITY + SEX + BIRTH_DATE + TYPE + ISSUE_COUNTRY + HOTEL + bestandsnaam + EXPIRY_DATE + PERSONAL_DATA + MRZ_FIELDS + MRZ1 + MRZ2 + MRZ_TYPE + MRZ_ISSUE_COUNTRY + MRZ_NAME + MRZ_DOCUMENT_NUMBER + MRZ_NATIONALITY + MRZ_SEX + MRZ_BIRTH_DATE + MRZ_EXPIRY_DATE + MRZ_PERSONAL_DATA + MRZ_FIELDS);
                }
            }
                       
            catch (SQLException ex){
                System.out.println("SQLException: " + ex.getMessage());
                System.out.println("SQLState: " + ex.getSQLState());
                System.out.println("VendorError: " + ex.getErrorCode());
                System.out.println("Databasefout in dbGetter!");
                                   }
            //return rs;  
           
       }
      
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by Kenjitsuka View Post
    The GUI class has a JTable in it, with a custom TableModel.
    I have a second class which exclusively makes the TableModel for me based on a resultset.
    The third class is one that gets the data from the database.

    My problem is, the database connection class gets a resultset (obviously), which I can read and print to the console (for testing purely).
    So the database connection and retrieval work, but it's a void class.
    What is a "void" class?

    Now I need it to return it's resultset to the TableModel class, which I can't seem to work.

    I've tried making it's main method a ResultSet instead of void, and added the line "return rs;" at the appropriate point.

    Then I get these errors:
    java.lang.NoSuchMethodError: main
    Exception in thread "main".
    ??? there should be no main method in the class that connects to the database. Instead why not make it a true object-oriented class with constructor, non-static private fields and public and private methods?

  3. #3
    Kenjitsuka is offline Member
    Join Date
    Oct 2010
    Posts
    18
    Rep Power
    0

    Default

    Sorry, I meant the class only has one method, main , and that is a void one. ( public static void main(String[] args) {} ).
    So it doesn't have a return type. What I need, is for it to return the ResultSet it generates.


    ??? there should be no main method in the class that connects to the database. Instead why not make it a true object-oriented class with constructor, non-static private fields and public and private methods?
    I am a beginner and the class as posted is the best I can come up with.
    Last edited by Kenjitsuka; 10-16-2010 at 07:15 PM.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by Kenjitsuka View Post
    Sorry, I meant the class only has one method, and that is a void one.

    I am a beginner and the class as posted is the best I can come up with.
    I think then that you've got the cart before the horse in that you really need to understand the rudiments of OOPs before attempting to tackle something as complex as GUI programming, much less integration of GUI and database. Speaking from experience, I know that when I tried to do GUI programming before being solidly grounded in the basics I failed miserably. I suggest that you may wish to place this project on hold and instead go through a book or the tutorials to learn about the object-oriented paradigm. You won't regret it.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Oh, and an excellent tutorial if you haven't gone through it are the Oracle Java tutorials which you can find here: Java Tutorials

    But to really get full bang for your buck with OOPs, you'll probably want to study a book devoted to object-oriented techniques. Check out Head First Object-Oriented Analysis and Design for more.

    Luck!

  6. #6
    Kenjitsuka is offline Member
    Join Date
    Oct 2010
    Posts
    18
    Rep Power
    0

    Default

    I'ved used Jigloo for Eclipse to generate the front end (GUI).
    I would say the hard part is all the back-end stuff, which I've eventually gotten down (unzipping, reading and parsing of XML files, writing the results to database etc.).

    Like I said, the only things left to do is tie the back end parts to the GUI.
    Since I can imagine people using data from database a lot in graphical programs I am surprised how little the API's provide, and how hard it is to get your ResultSet into a JTable. (Which seems to me the best/easiest way...).
    As well as how hard it is to find solutions via Google. Any special words I can try?

    I have books on JAVA, but they are from 2001 and not much use.
    Nonetheless I've been reading them again, and they go on and on about theory instead of showing real life examples. None of them even use the word database more than twice.

    I'm doing this project in my spare time to help a friend, not because I like programming. If his laptop wasn't so crappy I'd probably have tried using JSP, as I used to love how much easier it was to get your results on screen.

    Anyway, I'll keep looking and working on it, thanks!

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    I'm no database expert, but I would suggest that you use PreparedStatements when executing queries for several reasons including there are speed benefits from using pre-compiled statements and safety benefits as well. Consider creating a class that has no main method, but that has a constructor that takes your connection parameters. Then give it a method that has it execute a query with your prepared statement and simply have the method return the ResultSet. Then pass the ResultSet to the table model and iterate through it filling up the table rows.

    One bit of warning though, if you want to prevent your GUI from freezing and appearing very slow, do all your database querying off of the main Swing thread, the EDT or Event Dispatch Thread, and in a background thread such as a SwingWorker. If you are not familiar with these terms, please have a look here: Concurrency in Swing

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    One more thing before I forget. Rob Camick is one of the Swing experts you'll find on this forum from time to time who really knows Java and Swing backwards and forwards. If you check out his blog, you may find some helpful tips and code on getting info from a database into a JTable. Please have a look here table-from-database/ for instance.
    Last edited by Fubarable; 10-16-2010 at 07:54 PM.

  9. #9
    Kenjitsuka is offline Member
    Join Date
    Oct 2010
    Posts
    18
    Rep Power
    0

    Default

    One more thing before I forget. Rob Camick is one of the Swing experts you'll find on this forum from time to time. If you check out his blog, you may find some helpful tips and code on getting info from a database into a JTable. Please have a look here table-from-database/ for instance.
    I actually did come across that page, and it was very helpfull in creating my own TableModel class and tying it to my GUI.

    But like Rob says: "Of course it is still up to you to create the SQL and execute the query to create the ResultSet...".

    Working on it ;)

  10. #10
    Kenjitsuka is offline Member
    Join Date
    Oct 2010
    Posts
    18
    Rep Power
    0

    Default

    I've got it working! Very happy, after seven hours!

    I decided to get dirty, and simply get the ResultSet in the interface class.

Similar Threads

  1. accessing custom data model values - how?
    By dave247 in forum New To Java
    Replies: 11
    Last Post: 03-07-2010, 12:20 PM
  2. JTable view/model sync problem
    By edcincy in forum AWT / Swing
    Replies: 0
    Last Post: 09-12-2008, 06:25 PM
  3. Replies: 0
    Last Post: 08-29-2008, 12:49 PM
  4. Replies: 0
    Last Post: 08-29-2008, 12:16 PM
  5. ResultSet to JTable
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-11-2008, 09:01 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •