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

199 lines
8.3 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 JDBC classes to create and populate a table (part 2 of 2)" />
<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="jdbcex2" />
<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 JDBC classes to create and populate a table (part 2
of 2)</title>
</head>
<body id="jdbcex2"><a name="jdbcex2"><!-- --></a>
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
<h1 class="topictitle1">Example: Using JDBC classes to create and populate a table (part 2
of 2)</h1>
<div><p></p>
<div class="section"><p>[ <a href="jdbcex1.htm#jdbcex1">Previous part</a> ]</p>
<p>Use
the following as an example for your program.</p>
<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
//
// This source is an example of IBM Toolbox for Java JDBC driver.
//
//////////////////////////////////////////////////////////////////////////////////
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() &lt; width) {
StringBuffer buffer = new StringBuffer (s);
for (int i = s.length(); i &lt; 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 {
DriverManager.registerDriver(new com.ibm.as400.access.AS400JDBCDriver()); <a href="#jdbcex2__dup0030">Note 1 </a>
// 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 (); <a href="#jdbcex2__dup0031">Note 2 </a>
// Execute the query.
Statement select = connection.createStatement ();
ResultSet rs = select.executeQuery ("SELECT * FROM "
+ collectionName + dmd.getCatalogSeparator() + tableName); <a href="#jdbcex2__dup0032">Note 3 </a>
// 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 (); <a href="#jdbcex2__dup0033">Note 4 </a>
String[] columnLabels = new String[columnCount];
int[] columnWidths = new int[columnCount];
for (int i = 1; i &lt;= columnCount; ++i) {
columnLabels[i-1] = rsmd.getColumnLabel (i);
columnWidths[i-1] = Math.max (columnLabels[i-1].length(),
rsmd.getColumnDisplaySize (i)); <a href="#jdbcex2__dup0034">Note 5 </a>
}
// Output the column headings.
for (int i = 1; i &lt;= 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 &lt;= columnCount; ++i) {
for (int j = 1; j &lt;= 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 &lt;= columnCount; ++i) {
String value = rs.getString (i);
if (rs.wasNull ())
value = "&lt;null&gt;"; <a href="#jdbcex2__dup0035">Note 6 </a>
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 class="section"><ol><li id="jdbcex2__dup0030"><a name="jdbcex2__dup0030"><!-- --></a>This line loads the IBM<sup>®</sup> Toolbox for Java™ JDBC driver. A JDBC driver mediates
between JDBC and the database with which you are working.</li>
<li id="jdbcex2__dup0031"><a name="jdbcex2__dup0031"><!-- --></a>This line retrieves the connection's meta data, an object
that describes many of the characteristics of the database.</li>
<li id="jdbcex2__dup0032"><a name="jdbcex2__dup0032"><!-- --></a>This statement executes the query on the specified table.</li>
<li id="jdbcex2__dup0033"><a name="jdbcex2__dup0033"><!-- --></a>These lines retrieve information about the table.</li>
<li id="jdbcex2__dup0034"><a name="jdbcex2__dup0034"><!-- --></a>These lines set the column width to either the length of
the label or the length of the data, whichever is longer.</li>
<li id="jdbcex2__dup0035"><a name="jdbcex2__dup0035"><!-- --></a>This block of code iterates through all of the rows in the
table and displays the contents of each column in each row.</li>
</ol>
<p>[ <a href="jdbcex1.htm#jdbcex1">Previous part</a> ]</p>
</div>
</div>
</body>
</html>