112 lines
5.2 KiB
HTML
112 lines
5.2 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: ResultSetMetaData interface for IBM Developer Kit for Java" />
|
|
<meta name="DC.Relation" scheme="URI" content="codedisclaimer.htm" />
|
|
<meta name="DC.Relation" scheme="URI" content="rsltdata.htm" />
|
|
<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="rsmd" />
|
|
<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: ResultSetMetaData interface for IBM Developer Kit for Java</title>
|
|
</head>
|
|
<body id="rsmd"><a name="rsmd"><!-- --></a>
|
|
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
|
|
<h1 class="topictitle1">Example: ResultSetMetaData interface for IBM<sup>®</sup> Developer Kit for Java™</h1>
|
|
<div><div class="section"><p><strong>Note:</strong> Read the <a href="codedisclaimer.htm">Code
|
|
example disclaimer</a> for important legal information.</p>
|
|
<pre>import java.sql.*;
|
|
|
|
/**
|
|
ResultSetMetaDataExample.java
|
|
|
|
This program demonstrates using a ResultSetMetaData and
|
|
a ResultSet to display all the metadata about a ResultSet
|
|
created querying a table. The user passes the value for the
|
|
table and library in.
|
|
**/
|
|
public class ResultSetMetaDataExample {
|
|
|
|
public static void main(java.lang.String[] args)
|
|
{
|
|
if (args.length != 2) {
|
|
System.out.println("Usage: java ResultSetMetaDataExample <library> <table>");
|
|
System.out.println("where <library> is the library that contains <table>");
|
|
System.exit(0);
|
|
}
|
|
|
|
Connection con = null;
|
|
Statement s = null;
|
|
ResultSet rs = null;
|
|
ResultSetMetaData rsmd = null;
|
|
|
|
try {
|
|
// Get a database connection and prepare a statement.
|
|
Class.forName("com.ibm.db2.jdbc.app.DB2Driver");
|
|
con = DriverManager.getConnection("jdbc:db2:*local");
|
|
|
|
s = con.createStatement();
|
|
|
|
rs = s.executeQuery("SELECT * FROM " + args[0] + "." + args[1]);
|
|
rsmd = rs.getMetaData();
|
|
|
|
int colCount = rsmd.getColumnCount();
|
|
int rowCount = 0;
|
|
for (int i = 1; i <= colCount; i++) {
|
|
System.out.println("Information about column " + i);
|
|
System.out.println(" Name..........: " + rsmd.getColumnName(i));
|
|
System.out.println(" Data Type.....: " + rsmd.getColumnType(i) +
|
|
" ( " + rsmd.getColumnTypeName(i) + " )");
|
|
System.out.println(" Precision.....: " + rsmd.getPrecision(i));
|
|
System.out.println(" Scale.........: " + rsmd.getScale(i));
|
|
System.out.print (" Allows Nulls..: ");
|
|
if (rsmd.isNullable(i)==0)
|
|
System.out.println("false");
|
|
else
|
|
System.out.println("true");
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
// Handle any errors.
|
|
System.out.println("Oops... we have an error... ");
|
|
e.printStackTrace();
|
|
} finally {
|
|
// Ensure we always clean up. If the connection gets closed, the
|
|
// statement under it closes as well.
|
|
if (con != null) {
|
|
try {
|
|
con.close();
|
|
} catch (SQLException e) {
|
|
System.out.println("Critical error - cannot close connection object");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}</pre>
|
|
</div>
|
|
</div>
|
|
<div>
|
|
<div class="familylinks">
|
|
<div class="parentlink"><strong>Parent topic:</strong> <a href="rsltdata.htm" title="The ResultSet object provides several methods for obtaining column data for a row. All are of the form get<Type>, where <Type> is a Java data type. Some examples of these methods include getInt, getLong, getString, getTimestamp, and getBlob. Nearly all of these methods take a single parameter that is either the column index within the ResultSet or the column name.">Retrieve ResultSet data</a></div>
|
|
</div>
|
|
|
|
<div class="linklist"><strong>Collected links</strong><br />
|
|
|
|
<div><a href="codedisclaimer.htm">Code example disclaimer</a></div></div>
|
|
</div>
|
|
</body>
|
|
</html> |