SQLQueryBuilderPane Example


/////////////////////////////////////////////////////////////////////////
//
// 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 AS/400 Toolbox for Java "SQLQueryBuilderPane",
// and "SQLResultSetFormPane".
// IBM grants you a nonexclusive license to use this as an example
// from which you can generate similar function tailored to
// your own specific needs.
//
// This sample code is provided by IBM for illustrative purposes
// only. These examples have not been thoroughly tested under all
// conditions. IBM, therefore, cannot guarantee or imply
// reliability, serviceability, or function of these programs.
//
// All programs contained herein are provided to you "AS IS"
// without any warranties of any kind.  The implied warranties of
// merchantablility and fitness for a particular purpose are
// expressly disclaimed.
//
// AS/400 Toolbox for Java
// (C) Copyright IBM Corp. 1997, 1998
// All rights reserved.
// US Government Users Restricted Rights -
// Use, duplication, or disclosure restricted
// by GSA ADP Schedule Contract with IBM Corp.
//
/////////////////////////////////////////////////////////////////////////

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

public class SQLQueryBuilderPaneExample
{


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



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



    // The query builder pane.
    private static SQLQueryBuilderPane queryBuilderPane;



    public static 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 AS/400 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 static 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 ();
    }


}