Results 1 to 9 of 9
  1. #1
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    340
    Rep Power
    5

    Default JTables and Databases

    I am looking to create a Java Table (swing), and import the information from a database table. I have created the columns with:

    String[] columnNames = {"First Name",
    "Last Name",
    "Address",
    "City ",
    "Country",
    "Birth_Date"};

    From an example I was reading I needed the following:

    Object[][] data = {???};
    JTable table = new JTable(data, columnNames);

    I was going to put in the name of a function (getTable) in the data array which just does a select* to get the info from the table. Is that correct? If so, what arguments would I need to pass into the getTable(), and what would my sql function need to return.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default

    Check out Rob Camick's Table from Database
    Table From Database « Java Tips Weblog

    db

  3. #3
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    340
    Rep Power
    5

    Default

    for some reason its saying for:

    ListTableModel model = ListTableModel.createModelFromResultSet( resudltSet );

    "cannot find listtablemodel" and cannot find "resultSet"

    Im a little confused about the idea of a method returning a value.

    If I have the method that I called getData, which is a sql call to the database (select * from customers), then isnt that method "returning" the result set to the: ListTableModel model = ListTableModel.createModelFromResultSet( resudltSet ) to the resultSet field?

    I know alot of methods are public void meaning they dont return anything but in this case I need to get the result of the execute statement to pass as the resultSet right? so would it just be public getData()

  4. #4
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    JTable is one of several components needed to display a table in Swing. Another key component is the TableModel. You need to create a TableModel and assign it to the JTable. The TableModel holds the data displayed by the JTable. Look at Sun's Java Tutorial under the Swing section.

    @Admins Can we create a shortcut that adds a link the the tutorial? ;-)

  5. #5
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    340
    Rep Power
    5

    Default

    one more question..

    so I have this method:

    String[][] customer = null;

    private void get_fromDB() {
    customer = db.getTable();
    String[] l = new String[customer.length];
    for (int i = 0; i < customer.length; i++) {
    l[i] = customer[i][0];
    }
    }

    and the db.getTable() is supposed to return a string array(??)

    the db.getTable method looks like this:


    public String getTable() {
    Statement st = null;
    rs = null;
    try {
    String sql = "Select * from customer";
    rs = st.executeQuery(sql);
    System.out.println("done2");
    } catch (Exception e) {
    System.err.println("Got an exception! ");
    System.err.println(e.getMessage());
    }

    }

    i cant figure out what type of method I need to make this, or what to return. The getTable() requires a String [][] which im guessing is the data that comes back from my query, but how do I put that as the return statement?

    I tried to put return rs; but that didnt work since rs is a result set and I need a String [][]

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

    Default

    Your getTable's never going to work, at least until you actually have a Connection to the db, which you then use to create your Statement.

    That aside, you'd then cycle through your ResultSet, turning each row into an entry in the outer part of your String array, with each column becoming an entry in the inner part of the String array. Then return that String[][] object.

  7. #7
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    Quote Originally Posted by Tolls View Post
    Your getTable's never going to work, at least until you actually have a Connection to the db, which you then use to create your Statement.

    That aside, you'd then cycle through your ResultSet, turning each row into an entry in the outer part of your String array, with each column becoming an entry in the inner part of the String array. Then return that String[][] object.

    and also this one How to Use Tables (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components). if you have questions after reading these tutorials it will be much easier to answer. the concepts behind jtables and db connections are challanging enough and put altogether doesn't make it easier.

  8. #8
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    In general, I suggest avoiding using any sort of [][]. Instead, create a MyTableRow class that contains the fields that will be displayed in each row. Store the rows in a List inside the TableModel.

    When you read through your result set, create MyTableRow instances and add them to the List inside the TableModel.
    The Java Tutorial. Read it.

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

Similar Threads

  1. CheckBoxes and JTables
    By lakshayghai in forum AWT / Swing
    Replies: 1
    Last Post: 03-16-2010, 08:01 PM
  2. how to use XML databases
    By paty in forum XML
    Replies: 3
    Last Post: 08-16-2008, 12:11 AM
  3. Scrolling with JTables
    By hiranya in forum AWT / Swing
    Replies: 5
    Last Post: 10-30-2007, 08:48 AM
  4. Recommendation about databases
    By tommy in forum JDBC
    Replies: 2
    Last Post: 07-28-2007, 05:04 AM
  5. multiple databases
    By varunthecool in forum JDBC
    Replies: 2
    Last Post: 07-09-2007, 08:06 AM

Posting Permissions

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