Example: Using SQLQueryBuilderPane

Note: Read the Code example disclaimer for important legal information.
/////////////////////////////////////////////////////////////////////////
//
// SQLQueryBuilderPane example.  This program presents a query builder
// which allows the user to build a SQL query.
//
// Command syntax:
//    SQLQueryBuilderPaneExample system
//
// This source is an example of IBM Toolbox for Java "SQLQueryBuilderPane",
// and "SQLResultSetFormPane".
//
/////////////////////////////////////////////////////////////////////////

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 SQLQueryBuilderPaneExample
{


    // This connection is shared by all components.
    private SQLConnection connection;



    // This error handler is shared by all components.
    private ErrorDialogAdapter errorHandler;



    // The query builder pane.
    private SQLQueryBuilderPane queryBuilderPane;



    // This is the main java calls.  Here we create an instance of our
    // class and call our own Main() method.  We do this to avoid
    // problems with static.  Java has some restrictions with static
    // methods using non-static data, especially when it comes to
    // inner classes.  The code is cleaner if we keep the amount of
    // static data and methods to a minimum.
    public static void main (String[] args)
    {
       SQLQueryBuilderPaneExample me = new SQLQueryBuilderPaneExample();
       me.Main(args);
    }


    public void Main (String[] args)
    {
        // If a system was not specified, then display
        // help text and exit.
        if (args.length != 1)
        {
            System.out.println("Usage:  SQLQueryBuilderPaneExample system");
            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.
            connection = new SQLConnection ("jdbc:as400://" + args[0]);

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

            // Create an error dialog adapter.  This will display
            // any errors to the user.
            errorHandler = new ErrorDialogAdapter (f);

            // Create a SQL query builder pane to present the query
            // builder.  Load the data that is needed for the query
            // builder from the system.
            queryBuilderPane = new SQLQueryBuilderPane (connection);
            queryBuilderPane.addErrorListener (errorHandler);
            queryBuilderPane.load ();

            // Create a button which will display the results of
            // the generated query in a form pane in another frame.
            JButton resultSetButton = new JButton ("Show result set");
            resultSetButton.addActionListener (new ActionListener ()
            {
                public void actionPerformed (ActionEvent event)
                {
                    showFormPane (queryBuilderPane.getQuery ());
                }
            });

            // 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 ("Center", queryBuilderPane);
            f.getContentPane ().add ("South", resultSetButton);
            f.pack ();
            f.show ();
        }
        catch (Exception e)
        {
           System.out.println ("Error: " + e.getMessage ());
           System.exit (0);
        }
    }



    private void showFormPane (String query)
    {
        // Create a new frame for the results of the query.
        JFrame f = new JFrame (query);

        // Create a SQL result set form pane to present the results
        // of the query.  Load the results from the system.
        SQLResultSetFormPane formPane = new SQLResultSetFormPane (connection, query);
        formPane.addErrorListener (errorHandler);
        formPane.load ();

        // Layout the frame with the form pane.
        f.getContentPane ().setLayout (new BorderLayout ());
        f.getContentPane ().add ("Center", formPane);
        f.pack ();
        f.show ();
    }


}