简体   繁体   中英

How to get JTable data to update from ResultSet

I'm working on an address book program (just to practice coding) and I've hit a bit of a snag. The table is initially populated at launch from a result set, which is obtained via a method of another class. This is the method used to populate the list:

protected void populateContactList( int query ) 
{
    try
    {
        ResultSetMetaData metaData;
        contactListRS = contact.contactListQuery( query );
        metaData = contactListRS.getMetaData();
        int colCount = metaData.getColumnCount();

        String[] colTitlesArray = new String[] { 
                "ContactID", "Last Name", "First Name", "Company" };

        Vector< String > colTitles = new Vector< String >();
        colTitles.addAll( Arrays.asList( colTitlesArray ) );
        Vector< Vector< Object> > data = new Vector< Vector< Object > >();

        while ( contactListRS.next() )
        {
            Vector< Object > rowVector = new Vector< Object >();
            for ( int i = 1; i <= colCount; i++ )
            {
                rowVector.add( contactListRS.getObject( i ) );
            }
            data.add( rowVector );
        }

        contactTableModel.setDataVector( data, colTitles);

        // hides col 0 (contactID) from display on jtable
        // but retains data for retrieval from table model  
        contactDisplayTable.getColumnModel().getColumn( 0 )
            .setIdentifier( "contactID" );
        TableColumn contactIDCol = contactDisplayTable.
            getColumn( "contactID" );
        contactDisplayTable.getColumnModel().removeColumn( contactIDCol );

    }
    catch ( SQLException e )
    {
        System.err.println( e );
    }
}

This method works properly for the initial display of the data from the ResultSet with the initial query at launch, but after the ResultSet is changed as the result of a user search, the table is cleared and the new data is not displayed. This is the method used for the initial query:

protected ResultSet contactListQuery( int query )
{
    try 
    {
        if ( query == 0 )
        {   
            listQuery = conn.prepareStatement( queryArr[ query ] );
            queryRS = listQuery.executeQuery();
        }
        else if ( query == 1 )
        {               
            queryRS = searchContacts();
            queryRS.last();
        }

    }
    catch (SQLException e) 
    {
        e.printStackTrace();
    }
    return queryRS; 
}

This method is passed an int, indicating the calling method. If it is an initial query (or another call intended to display the full contact list), it is passed 0, resulting in a full list query. Otherwise it is passed 1, in which case it calls method searchContacts. In either case, it returns a ResultSet containing the same 4 elements from the DB to the populateContactList method. Below is the searchContacts method:

public ResultSet searchContacts() throws SQLException
    {
        String searchLN = addressBook.getSearchLN();
        String searchFN = addressBook.getSearchFN();
        String searchCO = addressBook.getSearchCO();

        boolean checkLN = searchLN.isEmpty();
        boolean checkFN = searchFN.isEmpty();
        boolean checkCO = searchCO.isEmpty();

        int rsLength;

        try 
        {
            if ( ( checkLN ) && ( checkFN ) && ( checkCO ) )
            {
                JOptionPane.showMessageDialog( addressBook.addressBookMainJF, 
                    "All fields are empty.\nPlease enter search parameters.", "Empty Search", JOptionPane.ERROR_MESSAGE );
                return contactListQuery( 0 );
            }
            else if ( !( checkLN ) && ( checkFN ) && ( checkCO ) )
            {
                listQuery = conn.prepareStatement( queryArr[ 1 ] );
                listQuery.setString( 1, searchLN );
            }
            else if ( ( checkLN ) && !( checkFN ) && ( checkCO ) )
            {
                listQuery = conn.prepareStatement( queryArr[ 2 ] );
                listQuery.setString( 1, searchFN );
            }
            else if ( !( checkLN ) && !( checkFN ) && ( checkCO ) )
            {
                listQuery = conn.prepareStatement( queryArr[ 3 ] );
                listQuery.setString( 1, searchLN );
                listQuery.setString( 2, searchFN ); 
            }
            else if ( ( checkLN ) && ( checkFN ) && !( checkCO ) )
            {
                listQuery = conn.prepareStatement( queryArr[ 4 ] );
                listQuery.setString( 1, searchCO );
            }
            else if ( ( checkLN ) && !( checkFN ) && !( checkCO ) )
            {
                listQuery = conn.prepareStatement( queryArr[ 5 ] );
                listQuery.setString( 1, searchFN );
                listQuery.setString( 2, searchCO );
            }
            else if ( !( checkLN ) && ( checkFN ) && !( checkCO ) )
            {
                listQuery = conn.prepareStatement( queryArr[ 6 ] );
                listQuery.setString( 1, searchLN );
                listQuery.setString( 2, searchCO );
            }
            else if ( !( checkLN ) && !( checkFN ) && !( checkCO ) )
            {
                listQuery = conn.prepareStatement( queryArr[ 7 ] );
                listQuery.setString( 1, searchLN );
                listQuery.setString( 2, searchFN );
                listQuery.setString( 3, searchCO );
            }
            else 
            {
                JOptionPane.showMessageDialog( addressBook.addressBookMainJF, 
                    "Error processing search.\nPlease try again.", "Search Error", JOptionPane.ERROR_MESSAGE );
            }

            searchRS = listQuery.executeQuery();
            searchRS.last();
            rsLength = searchRS.getRow();

            if ( rsLength < 1 )
            {
                return contactListQuery( 0 );
            }
        } 
        catch ( SQLException e ) 
        {

            e.printStackTrace();
        }
        searchRS.first();
        return searchRS;
    }

Obviously, this method determines which fields contain user input, populates the user input into the corresponding query, executes the query and returns the ResultSet. In any case, the same 4 elements are included in the ResultSet as the full list query.

I have tested to see if the result set contains data after the query, and it always does if there are any matches. I have also checked to make sure that the ResultSet is being returned to the populateContactList method, and it is. I believe the problem is somewhere in the populateContactList method, I just can't figure out where the issue lies. One theory I have is that the table may need to be refreshed via contactTableModel.fireTableDataChanged();, which I have tried at various points in the populateContactList method, but that did not resolve the issue. Perhaps there is something similar that will work I have not thought of?

I realize this is a lot to look through, I just wanted anyone willing to help to be able to see all the code involved, rather than having to guess at what it might look like. Any assistance provided with this will be greatly appreciated. I apologize in advance if any of this seems amateurish or if it is long winded. Just trying to be thorough and exact about the issue.

I figured it out on my own. Apparently the last bit before the catch block in the searchContacts method was throwing things off. I removed this bit:

searchRS.last(); 
rsLength = searchRS.getRow();
if ( rsLength < 1 ) 
{ 
    return contactListQuery( 0 ); 
}  

and now it's working properly. Just have to figure another way to handle an empty search result RS, but I'll probably be able to address that with a JOptionPane to notify the user there were no results found.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM