185 lines
6.7 KiB
HTML
185 lines
6.7 KiB
HTML
<?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 JDBCQuery to query a table" />
|
|
<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="jdbcqueryexample" />
|
|
<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 JDBCQuery to query a table</title>
|
|
</head>
|
|
<body id="jdbcqueryexample"><a name="jdbcqueryexample"><!-- --></a>
|
|
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
|
|
<h1 class="topictitle1">Example: Using JDBCQuery to query a table</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>
|
|
<pre>//////////////////////////////////////////////////////////////////////////////////
|
|
//
|
|
// JDBCQuery example. This program uses the IBM Toolbox for Java JDBC driver to
|
|
// query a table and output its contents.
|
|
//
|
|
// Command syntax:
|
|
// JDBCQuery system collectionName tableName
|
|
//
|
|
// For example,
|
|
// JDBCQuery MySystem qiws qcustcdt
|
|
//
|
|
//////////////////////////////////////////////////////////////////////////////////
|
|
|
|
import java.sql.*;
|
|
|
|
public class JDBCQuery
|
|
{
|
|
|
|
|
|
|
|
// Format a string so that it has the specified width.
|
|
private static String format (String s, int width)
|
|
{
|
|
String formattedString;
|
|
|
|
// The string is shorter than specified width,
|
|
// so we need to pad with blanks.
|
|
if (s.length() < width) {
|
|
StringBuffer buffer = new StringBuffer (s);
|
|
for (int i = s.length(); i < width; ++i)
|
|
buffer.append (" ");
|
|
formattedString = buffer.toString();
|
|
}
|
|
|
|
// Otherwise, we need to truncate the string.
|
|
else
|
|
formattedString = s.substring (0, width);
|
|
|
|
return formattedString;
|
|
}
|
|
|
|
|
|
|
|
public static void main (String[] parameters)
|
|
{
|
|
// Check the input parameters.
|
|
if (parameters.length != 3) {
|
|
System.out.println("");
|
|
System.out.println("Usage:");
|
|
System.out.println("");
|
|
System.out.println(" JDBCQuery system collectionName tableName");
|
|
System.out.println("");
|
|
System.out.println("");
|
|
System.out.println("For example:");
|
|
System.out.println("");
|
|
System.out.println("");
|
|
System.out.println(" JDBCQuery mySystem qiws qcustcdt");
|
|
System.out.println("");
|
|
return;
|
|
}
|
|
|
|
String system = parameters[0];
|
|
String collectionName = parameters[1];
|
|
String tableName = parameters[2];
|
|
|
|
Connection connection = null;
|
|
|
|
try {
|
|
|
|
// Load the IBM Toolbox for Java JDBC driver.
|
|
DriverManager.registerDriver(new com.ibm.as400.access.AS400JDBCDriver());
|
|
|
|
// Get a connection to the database. Since we do not
|
|
// provide a user id or password, a prompt will appear.
|
|
connection = DriverManager.getConnection ("jdbc:as400://" + system);
|
|
DatabaseMetaData dmd = connection.getMetaData ();
|
|
|
|
// Execute the query.
|
|
Statement select = connection.createStatement ();
|
|
ResultSet rs = select.executeQuery (
|
|
"SELECT * FROM " + collectionName + dmd.getCatalogSeparator() + tableName);
|
|
|
|
// Get information about the result set. Set the column
|
|
// width to whichever is longer: the length of the label
|
|
// or the length of the data.
|
|
ResultSetMetaData rsmd = rs.getMetaData ();
|
|
int columnCount = rsmd.getColumnCount ();
|
|
String[] columnLabels = new String[columnCount];
|
|
int[] columnWidths = new int[columnCount];
|
|
for (int i = 1; i <= columnCount; ++i) {
|
|
columnLabels[i-1] = rsmd.getColumnLabel (i);
|
|
columnWidths[i-1] = Math.max (columnLabels[i-1].length(), rsmd.getColumnDisplaySize (i));
|
|
}
|
|
|
|
// Output the column headings.
|
|
for (int i = 1; i <= columnCount; ++i) {
|
|
System.out.print (format (rsmd.getColumnLabel(i), columnWidths[i-1]));
|
|
System.out.print (" ");
|
|
}
|
|
System.out.println ();
|
|
|
|
// Output a dashed line.
|
|
StringBuffer dashedLine;
|
|
for (int i = 1; i <= columnCount; ++i) {
|
|
for (int j = 1; j <= columnWidths[i-1]; ++j)
|
|
System.out.print ("-");
|
|
System.out.print (" ");
|
|
}
|
|
System.out.println ();
|
|
|
|
// Iterate throught the rows in the result set and output
|
|
// the columns for each row.
|
|
while (rs.next ()) {
|
|
for (int i = 1; i <= columnCount; ++i) {
|
|
String value = rs.getString (i);
|
|
if (rs.wasNull ())
|
|
value = "<null>";
|
|
System.out.print (format (value, columnWidths[i-1]));
|
|
System.out.print (" ");
|
|
}
|
|
System.out.println ();
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception e) {
|
|
System.out.println ();
|
|
System.out.println ("ERROR: " + e.getMessage());
|
|
}
|
|
|
|
finally {
|
|
|
|
// Clean up.
|
|
try {
|
|
if (connection != null)
|
|
connection.close ();
|
|
}
|
|
catch (SQLException e) {
|
|
// Ignore.
|
|
}
|
|
}
|
|
|
|
System.exit (0);
|
|
}
|
|
|
|
|
|
|
|
}</pre>
|
|
</div>
|
|
</div>
|
|
|
|
</body>
|
|
</html> |