-
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??
-
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.
-
Heres My code so far....
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
-
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!