ibm-information-center/dist/eclipse/plugins/i5OS.ic.rzahh_5.4.0.1/sqlresultsettablepaneexample.htm

187 lines
7.3 KiB
HTML
Raw Normal View History

2024-04-02 14:02:31 +00:00
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en-us" xml:lang="en-us">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="security" content="public" />
<meta name="Robots" content="index,follow" />
<meta http-equiv="PICS-Label" content='(PICS-1.1 "http://www.icra.org/ratingsv02.html" l gen true r (cz 1 lz 1 nz 1 oz 1 vz 1) "http://www.rsac.org/ratingsv01.html" l gen true r (n 0 s 0 v 0 l 0) "http://www.classify.org/safesurf/" l gen true r (SS~~000 1))' />
<meta name="DC.Type" content="reference" />
<meta name="DC.Title" content="Example: Using SQLResultSetTablePane" />
<meta name="abstract" content="" />
<meta name="description" content="" />
<meta name="copyright" content="(C) Copyright IBM Corporation 2006" />
<meta name="DC.Rights.Owner" content="(C) Copyright IBM Corporation 2006" />
<meta name="DC.Format" content="XHTML" />
<meta name="DC.Identifier" content="sqlresultsettablepaneexample" />
<meta name="DC.Language" content="en-us" />
<!-- All rights reserved. Licensed Materials Property of IBM -->
<!-- US Government Users Restricted Rights -->
<!-- Use, duplication or disclosure restricted by -->
<!-- GSA ADP Schedule Contract with IBM Corp. -->
<link rel="stylesheet" type="text/css" href="./ibmdita.css" />
<link rel="stylesheet" type="text/css" href="./ic.css" />
<title>Example: Using SQLResultSetTablePane</title>
</head>
<body id="sqlresultsettablepaneexample"><a name="sqlresultsettablepaneexample"><!-- --></a>
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
<h1 class="topictitle1">Example: Using SQLResultSetTablePane</h1>
<div><p></p>
<div class="section"><div class="note"><span class="notetitle">Note:</span> Read the <a href="codedisclaimer.htm#codedisclaimer">Code
example disclaimer</a> for important legal information.</div>
</div>
<div class="section"><div class="p"><pre>/////////////////////////////////////////////////////////////////////////
//
// 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);
}
}
}</pre>
</div>
</div>
</div>
</body>
</html>