Displaying the results of a MySQL Query in a NetBeans JTable

How would I go about displaying the results of a simple MySQL select statement in a JTable component in NetBeans 6.0?

Something like this:

select ID, FIRST, LAST from table where variable = 'Y';

Any help would be appreciated!

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

If you are using matisse there are a couple of nice tutorials you can use:

The one above is using persistence. If you do not wish to use Persistence and just use the JDBC driver you can go like this:

/*Assuming your JTABLE is called myTable*/

    Object[][] Data = null;
    try {
      String Query = "select ID, FIRST, LAST from table where variable = 'Y";;
      ResultSet rs = myJDBCConnection.createStatement().executeQuery(Query);
      int Size = rs.getRow();
      if (netSize > 0) {
        Data = new Object[Size][3];
        for (int j = 0; j < Size; j++) {
          Data[j][0] = rs.getString("ID");
          Data[j][1] = rs.getString("First");
          Data[j][1] = rs.getString("Last");

    catch (Exception e) {
    mytable.setModel(new javax.swing.table.DefaultTableModel(
        new String[] {
        "ID", "First", "Last"    }
        ) {
      Class[] types = new Class[] {
          java.lang.String.class, java.lang.String.class, java.lang.String.class};
      boolean[] canEdit = new boolean[] {
          false, false, false

      public Class getColumnClass(int columnIndex) {
        return types[columnIndex];

      public boolean isCellEditable(int rowIndex, int columnIndex) {
        return canEdit[columnIndex];

Hope this helps

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Titanfan21Author Commented:
Wow, very cool.

What I did was go into the properties of the empty JTable and clicked on 'Customize Code.'  Then in the second dropbox on the left I chose 'custom property', where I pasted the stuff in there over what was already there.  That's what I'm supposed to do, right?
Here were a couple other quick questions I had:

1) You start out the code with 'Object', but what was originally there said 'new Object.'  Should I do it like that, or just leave it as Object.

2) At the end of the line that reads:
String Query = "select ID, FIRST, LAST from table where variable = 'Y";;
You put two semicolons.  Should there be just one, or two?

3) When I try to run the project to test it out, it says:
Class "myproject.TableTest" does not have a main method.
How would I fix that so it will run?

Thanks so much for your help!


Also, it won't let me run the program as it says
Titanfan21Author Commented:
Ok.  I've been fooling around with this a little bit and am getting one error when I try to run it:

(File Path Here).java:275: array dimension missing
            new Object[][] Data = null;
1 error
BUILD FAILED (total time: 0 seconds)

Any idea what this means or how to fix it?

If I look in the source, there looks to be a few other errors as well.

cannot find symbol
symbol : variable my JDBCConnection
symbol : method createStatement()
symbol : variable netSize

operator > cannot be implied to netSize, int

cannot find symbol
variable : Data
symbol : constructor DefaultTableModel(Data,java.lang.String[])
variable: mytable

Not sure if those will matter once the main error that it gives when I try to run it gets fixed.

Thanks so much!

Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

Sorry for the delay:

1) Left Click - > Fix Imports should decrease the errors.
2) My code sample assumes that you have basic experience in using mysql JDBC from java. This means
that you still have to initialize the driver and connect to the database yourself.
"myJDBCConnection" needs to be a java.sql.Connection Object ready to create statements.
3) netSize is a typo, should be size.
4) You need to create a main method. If netbeans hasn't already done that for you (which should in Project creation) then you need to choose which is your starter class and add the a main method (Java Basics).

Titanfan21Author Commented:
Thanks!  I did all of that, but I'm still getting the primary error when I try to run it.

(File Path Here).java:275: array dimension missing
            new Object[][] Data = null;
1 error
BUILD FAILED (total time: 0 seconds)

Do you know why it's doing that?


As it says you are trying to construct an array ( new Obect[][] ) with no dimension.
I feel that you wanted to type something like Obect[][] Data = null;
Forced accept.

EE Admin
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.