Results 1 to 3 of 3
  1. #1
    smallmos1 is offline Member
    Join Date
    Sep 2010
    Posts
    23
    Rep Power
    0

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

    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);
    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?).

    all help welcome, thanks

  2. #2
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    4

    Default

    Do you receive any error? What is the error message?

  3. #3
    jasonwucinski is offline Member
    Join Date
    Feb 2011
    Posts
    64
    Rep Power
    0

    Default

    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:
    Java Code:
    stmt = myConnection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
    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:
    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

  1. Jtable cells not populating correctly
    By wlc in forum AWT / Swing
    Replies: 2
    Last Post: 04-04-2011, 05:57 AM
  2. Populating Jtable from database - better way?
    By madhura2212 in forum AWT / Swing
    Replies: 5
    Last Post: 10-05-2010, 02:04 PM
  3. Populating JTable with 2 arrays (Netbeans)
    By althair in forum AWT / Swing
    Replies: 3
    Last Post: 12-30-2009, 02:05 PM
  4. Populating a JTable
    By toymachiner62 in forum New To Java
    Replies: 2
    Last Post: 10-13-2009, 05:56 AM
  5. Replies: 11
    Last Post: 02-20-2009, 02:10 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
  •