0

I'm having a strange problem with the below code, it works fine when its run without the if else statements, but displays no results in the jtable when if else is used. Is there something stupid I'm missing here?

        try {
        Class.forName(dbClass);
        Connection con = DriverManager.getConnection (dbUrl,dbUsername, dbPassword);
        Statement stmt = con.createStatement();

        String userQuery = "SELECT p_id AS 'Patient ID', forename AS 'Forename', surname AS 'Surname', address AS 'Address' FROM Patient WHERE surname LIKE '%"+s+"%'";
        ResultSet userResult = stmt.executeQuery(userQuery);

        if(!userResult.next())
        {
            JOptionPane.showMessageDialog(null, "No Results.");
        {
        else{
        ResultSetMetaData rsMetaData =userResult.getMetaData();
        DefaultTableModel dtm = new DefaultTableModel();
        int cols = rsMetaData.getColumnCount();
        Vector colName = new Vector();
        Vector dataRows = new Vector();

        for (int i=1; i<cols; i++){
        colName.addElement(rsMetaData.getColumnName(i));
        }
        dtm.setColumnIdentifiers(colName);

        while(userResult.next()){
            dataRows = new Vector();
            for(int j = 1; j<cols; j++){
                dataRows.addElement(userResult.getString(j));
            }
            dtm.addRow(dataRows);
        }
        searchTable.setModel(dtm);
        con.close();
        }
    } //end try

    catch(ClassNotFoundException e) {
        JOptionPane.showMessageDialog(null, "Database Error.");
        e.printStackTrace();
    }

    catch(SQLException e) {
        JOptionPane.showMessageDialog(null, "Database Error.");
        e.printStackTrace();
    }

I'm using netbeans for the GUI.

Thanks

2 Answers 2

1

The connection object (con) should be closed outside if/else block.

Beside, the userResult.next() was called twice in the else statement block..

You may fix it by replacing while() by do while loop:

do {
        dataRows = new Vector();
        for (int j = 1; j < cols; j++) {
            dataRows.addElement(userResult.getString(j));
        }
            dtm.addRow(dataRows);
    }
while (userResult.next());
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the reply, still not working. Here's a sample of the method, any idea whats going wrong? It's actually displaying the column headers correctly, but no content in the cells. There's definitely content to display, and it works fine without the if /else block pastebin.com/RHYpGEAg
0

Please include finally to handle closing the connection and removing the other resources.

1 Comment

Thanks for the reply, still not working and finnally block is included. Here's a sample of the method, any idea whats going wrong? It's actually displaying the column headers correctly, but no content in the cells. There's definitely content to display, and it works fine without the if /else block pastebin.com/RHYpGEAg

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.