Results 1 to 4 of 4
  1. #1
    blu_fox88 is offline Member
    Join Date
    Feb 2010
    Posts
    9
    Rep Power
    0

    Default Working with Jtable

    I have made a connection to MS Access in java and can retrieve information..
    I have 2 questions...
    How do I search for specific data in table and display in a jTable..for example if my database contained concert events how would I pick all events that are on a certain date and display them in a Jtable??

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

    Default

    To pick all the events, you'll need to know how to do database queries. To display them, you'd create a table that uses a DefaultTableModel (avoid using an AbstractTableModel if you can) that has appropriate columns to match the data you want to display. Then you would create an Object[] whose size matches the table's column count, fill the array with info from the result set, add it to the table's default table model via the addRow(Object[]) method, and you're done.

    For more details, please check out the Sun Java tutorials.

  3. #3
    blu_fox88 is offline Member
    Join Date
    Feb 2010
    Posts
    9
    Rep Power
    0

    Default

    Heres My code so far....

    Java Code:
    try {
                String Curren = (String)jComboBox1.getSelectedItem();
                String YearC = (String)jComboBox2.getSelectedItem();
                String MonthC = (String)jComboBox3.getSelectedItem();
                String DayC = (String)jComboBox4.getSelectedItem();
                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                String filename = "E:/Database11.mdb";
                String database = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=";
                database+= filename.trim() + ";DriverID=22;READONLY=true}";
                Connection con = DriverManager.getConnection( database ,"","");
                Statement s = con.createStatement();
            s.execute("select Year from Rop"); 
            ResultSet rs = s.getResultSet();
            if (rs.next())
                ????.setText(rs.getString(1));
            s.execute("select Month from Rop"); 
            rs = s.getResultSet(); 
            if (rs.next())
                ????.setText(rs.getString(1));
            s.execute("select Day from Rop"); 
            rs = s.getResultSet(); 
            if (rs.next())
               ?????.setText(rs.getString(1));
            }
            s.execute("drop table Rop");
            s.close(); 
            con.close();
    
              }
    
    
                catch (Exception e) {
                System.out.println("Error: " + e);
            }
    
        }
    ------------------------------------------------------------------------------------
    is there any way I can just replace the ????? with something that will populate the table??
    and how would i search for things in the table that are similar to what the user chooses in the combo boxes...???


    moderator edit: code tags added to aid readability
    Last edited by Fubarable; 02-20-2010 at 04:46 PM. Reason: Code tags added

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

    Default

    You don't use "setText" Again, you want to create an array of Objects, populate this array with the info extracted from your database (the info you're trying to add to something via ???.setText), and then once you have the Object array fully populated, call addRow(yourObjectArrayGoesHere) on the JTable's DefaultTableModel.

    Again, I strongly urge you to read the Sun tutorial on using table models as well as search this and other fora for examples of its use.

    Also, I hope you don't mind but I added code tags to your code posted above. To learn to do this in your next post, please read my signature below.

    Much luck!

Similar Threads

  1. working with JC
    By yuhobebbho in forum New To Java
    Replies: 0
    Last Post: 02-10-2010, 11:22 PM
  2. Adding New JTable in JTable
    By anilkumar_vist in forum New To Java
    Replies: 0
    Last Post: 01-27-2010, 08:27 AM
  3. Replies: 8
    Last Post: 05-28-2008, 07:00 AM
  4. Replies: 2
    Last Post: 05-15-2008, 10:15 AM
  5. Working With ANT
    By JavaForums in forum Eclipse
    Replies: 0
    Last Post: 04-26-2007, 08:16 PM

Posting Permissions

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