SQLResultSetTablePane Example
/////////////////////////////////////////////////////////////////////////
//
// 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 AS/400 Toolbox for Java "SQLQueryBuilderPane",
// "SQLResultSetFormPane", and "SQLStatementButton".
// 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 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 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. 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)
{
document.execute ();
tablePane.load ();
}
}
});
// 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);
}
}
}