Example: Using SQLResultSetTablePane

Note: Read the Code example disclaimer for important legal information.
/////////////////////////////////////////////////////////////////////////
//
// SQLResultSetTablePane example.  This program presents the contents of
// a table in a table pane.  There is a SQLStatementDocument which allows
// the user to type in any SQL statement.  In addition, there is a button
// which allows the user to delete all rows of the table.
//
// Command syntax:
//    SQLResultSetTablePaneExample  system  table
//
// This source is an example of IBM Toolbox for Java "SQLQueryBuilderPane",
// "SQLResultSetFormPane", and "SQLStatementButton".
//
/////////////////////////////////////////////////////////////////////////

import com.ibm.as400.access.*;
import com.ibm.as400.vaccess.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;

public class SQLResultSetTablePaneExample
{



    private static SQLStatementDocument     document;
    private static SQLResultSetTablePane    tablePane;



    public static void main (String[] args)
    {
        // If a system was not specified, then display
        // help text and exit.
        if (args.length != 2)
        {
            System.out.println("Usage:  SQLResultSetTablePaneExample  system  table");
            return;
        }

        try
        {
            // Register the IBM Toolbox for Java JDBC driver.
            DriverManager.registerDriver (new AS400JDBCDriver ());

            // Create an SQLConnection object.  The system name was passed
            // as the first command line argument.  This connection is
            // shared by all components.
            SQLConnection connection = new SQLConnection ("jdbc:as400://" + args[0]);

            // Create a frame.
            JFrame f = new JFrame ("SQLResultSetTablePane example");

            // Create an error dialog adapter.  This will display
            // any errors to the user.  This error handler is shared
            // by all components.
            ErrorDialogAdapter errorHandler = new ErrorDialogAdapter (f);

            // Create a SQL statement document which allows the
            // user to enter a query.
            document = new SQLStatementDocument (connection, "");
            document.addErrorListener (errorHandler);

            // Create a text field for presenting the document.
            JTextField textField = new JTextField (document,
                "Enter a SQL statement here.", 50);

            // Create a button that deletes all rows of the table.
            SQLStatementButton deleteAllButton = new SQLStatementButton ("Delete all rows");
            deleteAllButton.setConnection (connection);
            deleteAllButton.setSQLStatement ("DELETE FROM " + args[1]);
            deleteAllButton.addErrorListener (errorHandler);

            // Create a SQL result set table pane to present the results
            // of a query.  Load the contents immediately.
            tablePane = new SQLResultSetTablePane (connection, "SELECT * FROM " + args[1]);
            tablePane.addErrorListener (errorHandler);
            tablePane.load ();

            // When enter is pressed in the text field,
            // execute the SQL statement and update the table pane.
            textField.addKeyListener (new KeyAdapter ()
            {
                public void keyPressed (KeyEvent event)
                {
                    if (event.getKeyCode () == KeyEvent.VK_ENTER)
                    {
                        // If the SQL statement is a SELECT, then
                        // let the table pane execute it, otherwise,
                        // let the document execute it.
                        String sql = document.getSQLStatement ();
                        if (sql.toUpperCase ().startsWith ("SELECT"))
                        {
                            try
                            {
                                tablePane.setQuery (sql);
                            }
                            catch (Exception e)
                            {
                                // Ignore.
                            }
                            tablePane.load ();
                        }
                        else
                            document.execute ();
                    }
                }
            });

            // When all rows are deleted using the button, then
            // update the table pane.
            deleteAllButton.addActionCompletedListener (new ActionCompletedListener ()
            {
                public void actionCompleted (ActionCompletedEvent event)
                {
                    tablePane.load ();
                }
            });

            // When the frame closes, exit.
            f.addWindowListener (new WindowAdapter ()
            {
                public void windowClosing (WindowEvent event)
                {
                    System.exit (0);
                }
            });

            // Layout the frame with the query builder pane.
            f.getContentPane ().setLayout (new BorderLayout ());
            f.getContentPane ().add ("North", textField);
            f.getContentPane ().add ("Center", tablePane);
            f.getContentPane ().add ("South", deleteAllButton);
            f.pack ();
            f.show ();
        }
        catch (Exception e)
        {
           System.out.println ("Error: " + e.getMessage ());
           System.exit (0);
        }
    }



}