• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 6607
  • Last Modified:

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!

Mark
0
Titanfan21
Asked:
Titanfan21
  • 3
  • 3
1 Solution
 
NelliosCommented:
If you are using matisse there are a couple of nice tutorials you can use:
http://www.netbeans.org/kb/60/java/gui-db.html

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);
      rs.last();
      int Size = rs.getRow();
      rs.first();
      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");
          rs.next();
        }
      }

    }
    catch (Exception e) {
      e.printStackTrace(System.out);
    }
    mytable.setModel(new javax.swing.table.DefaultTableModel(
        Data,
        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
0
 
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!

Mark

Also, it won't let me run the program as it says
0
 
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!

Mark
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
NelliosCommented:
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).

0
 
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?

Thanks!

Mark
0
 
NelliosCommented:
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;
0
 
Computer101Commented:
Forced accept.

Computer101
EE Admin
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now