Results 1 to 3 of 3
- 04-29-2011, 12:23 PM #1
Member
- Join Date
- Sep 2010
- Posts
- 23
- Rep Power
- 0
populating jtable with resultSet - help needed
trying to populate a jtable with a resultSet from a query. the method takes two sql date types in....been searching around a lot and finding it hard to get working, anyone point me in the right direction?
the code for putting the resultSet and jtable is a bit messy i know. basically user will select two dates, start and end, and when they hit the submit button (in the gui class), i want the query to execute, and the resultSet to appear in a jtable in a new window (would it be better to have a results class that displays this jtable, or is ok to have it appear as i have a above?).Java Code:public static void createResult(java.sql.Date jsqlStartDate, java.sql.Date jsqlEndDate)throws SQLException { String query = "SELECT s.shift_date, st.start_time, st.end_time FROM Shift s, Shift_Time st WHERE s.shift_time_id = st.shift_time_id AND st.shift_date > ? AND st.shift_date < ?"; pstmt = conn.prepareStatement(query); pstmt.setDate(1,jsqlStartDate); pstmt.setDate(2,jsqlEndDate); Rset = pstmt.executeQuery(); ResultSetMetaData rsmd = Rset.getMetaData(); int columnCount = rsmd.getColumnCount(); Vector columns = new Vector(columnCount); for(int i = 1; i < columnCount; i++) { columns.add(rsmd.getColumnName(i)); } Vector data = new Vector(); while(Rset.next()) { Vector row = new Vector(columnCount); for(int i = 1; i <=columnCount; i++) { row.add(Rset.getString(i)); } data.add(row); } Rset.close(); pstmt.close(); JTable table = new JTable(data, columns); JScrollPane scrollPane = new JScrollPane( table ); Container cPane = null; cPane.add( scrollPane, table ); table.setVisible(true);
all help welcome, thanks
- 04-30-2011, 01:36 AM #2
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
Do you receive any error? What is the error message?
- 05-07-2011, 06:32 PM #3
Member
- Join Date
- Feb 2011
- Posts
- 64
- Rep Power
- 0
i'm still new working with java but here's some of my limited insights, take them for what theyre worth. First, are you're sure your sql string is producing the desired results? Second, by default, result sets are only one direction. you can change this by using this:
and lastly, i'm sure theres plenty of ways to create a jTable but I was under the impression you needed to create a DefaultTableModel to a jTable and add the columns to that? below is how I add data to a jTable, maybe that exampe can help. good luck.Java Code:stmt = myConnection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
Java Code:private void loadPatients(){ String SQL = "SELECT PatientID, PatientFName,PatientLName,PatientDOB,PatientGender FROM tblPatient ORDER BY PatientLName ASC"; String curDir = System.getProperty("user.dir"); String myUrl = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=" + curDir + "\\" + "ADR_DB.accdb"; String username = ""; String password = ""; Connection myConnection; Statement stmt; ResultSet rs; int myCounter = 0; try { myConnection = DriverManager.getConnection(myUrl,username,password); stmt = myConnection.createStatement(); rs = stmt.executeQuery(SQL); DefaultTableModel model = new DefaultTableModel(); this.jTable1.setModel(model); model.addColumn("PatientID"); model.addColumn("First Name"); model.addColumn("Last Name"); model.addColumn("DOB"); model.addColumn("Gender"); jTable1.setAutoResizeMode(0); try { while ( rs.next() ) { this.patientID = rs.getString(1); this.patientFName = rs.getString(2); this.patientLName = rs.getString(3); this.patientDOB = rs.getString(4); this.patientGender = rs.getString(5); model.addRow(new Object[]{ this.patientID, this.patientFName, this.patientLName, this.patientDOB,this.patientGender}); } } catch (Exception e) { callingFrame.setColor(Color.red); callingFrame.setLable("Error Filling Combo:" + e.toString()); } finally { rs.close(); stmt.close(); myConnection.close(); } } catch (Exception e) { callingFrame.setColor(Color.red); callingFrame.setLable("Error Filling Combo:" + e.toString()); } }
Similar Threads
-
Jtable cells not populating correctly
By wlc in forum AWT / SwingReplies: 2Last Post: 04-04-2011, 05:57 AM -
Populating Jtable from database - better way?
By madhura2212 in forum AWT / SwingReplies: 5Last Post: 10-05-2010, 02:04 PM -
Populating JTable with 2 arrays (Netbeans)
By althair in forum AWT / SwingReplies: 3Last Post: 12-30-2009, 02:05 PM -
Populating a JTable
By toymachiner62 in forum New To JavaReplies: 2Last Post: 10-13-2009, 05:56 AM -
Populating a combo-box in a JFrame with resultset from database
By matpj in forum AWT / SwingReplies: 11Last Post: 02-20-2009, 02:10 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks