169 lines
7.2 KiB
HTML
169 lines
7.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: Using JDBC classes to create and populate a table (part 1 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="jdbcex1" />
|
|
<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 1
|
|
of 2)</title>
|
|
</head>
|
|
<body id="jdbcex1"><a name="jdbcex1"><!-- --></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 1
|
|
of 2)</h1>
|
|
<div><p></p>
|
|
<div class="section"><p>[ <a href="jdbcex2.htm#jdbcex2">Next 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>//////////////////////////////////////////////////////////////////////////////////
|
|
//
|
|
// JDBCPopulate example. This program uses the IBM Toolbox for Java JDBC driver
|
|
// to create and populate a table.
|
|
//
|
|
// Command syntax:
|
|
// JDBCPopulate system collectionName tableName
|
|
//
|
|
// For example,
|
|
// JDBCPopulate MySystem MyLibrary MyTable
|
|
//
|
|
// This source is an example of IBM Toolbox for Java JDBC driver.
|
|
//
|
|
//////////////////////////////////////////////////////////////////////////////////
|
|
|
|
import java.sql.*;
|
|
|
|
public class JDBCPopulate
|
|
{
|
|
|
|
private static final String words[]
|
|
= { "One", "Two", "Three", "Four", "Five",
|
|
"Six", "Seven", "Eight", "Nine", "Ten",
|
|
"Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen",
|
|
"Sixteen", "Seventeen","Eighteen", "Nineteen", "Twenty" };
|
|
|
|
public static void main (String[] parameters)
|
|
{
|
|
|
|
if (parameters.length != 3) {
|
|
System.out.println("");
|
|
System.out.println("Usage:");
|
|
System.out.println("");
|
|
System.out.println(" JDBCPopulate system collectionName tableName");
|
|
System.out.println("");
|
|
System.out.println("");
|
|
System.out.println("For example:");
|
|
System.out.println("");
|
|
System.out.println("");
|
|
System.out.println(" JDBCPopulate MySystem MyLibrary MyTable");
|
|
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="#jdbcex1__dup0023">Note 1 </a>
|
|
|
|
|
|
connection = DriverManager.getConnection ("jdbc:as400://"
|
|
+ system + "/" + collectionName); <a href="#jdbcex1__dup0024">Note 2 </a>
|
|
|
|
try {
|
|
Statement dropTable = connection.createStatement ();
|
|
dropTable.executeUpdate ("DROP TABLE " + tableName); <a href="#jdbcex1__dup0025">Note 3 </a>
|
|
}
|
|
catch (SQLException e) {
|
|
}
|
|
|
|
|
|
Statement createTable = connection.createStatement ();
|
|
createTable.executeUpdate ("CREATE TABLE " + tableName
|
|
+ " (I INTEGER, WORD VARCHAR(20), SQUARE INTEGER, "
|
|
+ " SQUAREROOT DOUBLE)"); <a href="#jdbcex1__dup0026">Note 4 </a>
|
|
|
|
|
|
PreparedStatement insert = connection.prepareStatement ("INSERT INTO "
|
|
+ tableName + " (I, WORD, SQUARE, SQUAREROOT) "
|
|
+ " VALUES (?, ?, ?, ?)"); <a href="#jdbcex1__dup0027">Note 5 </a>
|
|
|
|
|
|
for (int i = 1; i <= words.length; ++i) {
|
|
insert.setInt (1, i);
|
|
insert.setString (2, words[i-1]);
|
|
insert.setInt (3, i*i);
|
|
insert.setDouble (4, Math.sqrt(i));
|
|
insert.executeUpdate (); <a href="#jdbcex1__dup0028">Note 6 </a>
|
|
}
|
|
|
|
System.out.println ("Table " + collectionName + "." + tableName
|
|
+ " has been populated.");
|
|
}
|
|
|
|
catch (Exception e) {
|
|
System.out.println ();
|
|
System.out.println ("ERROR: " + e.getMessage());
|
|
}
|
|
|
|
finally {
|
|
|
|
try {
|
|
if (connection != null)
|
|
connection.close (); <a href="#jdbcex1__dup0029">Note 7 </a>
|
|
}
|
|
catch (SQLException e) {
|
|
// Ignore.
|
|
}
|
|
}
|
|
|
|
System.exit (0);
|
|
}
|
|
|
|
}</pre>
|
|
</div>
|
|
<div class="section"><ol><li id="jdbcex1__dup0023"><a name="jdbcex1__dup0023"><!-- --></a>This line loads the IBM<sup>®</sup> Toolbox for Java™ JDBC driver. A JDBC driver is necessary
|
|
to mediate between JDBC and the database you are working with.</li>
|
|
<li id="jdbcex1__dup0024"><a name="jdbcex1__dup0024"><!-- --></a>This statement connects to the database. A prompt will appear
|
|
for the user ID and password. A default schema is provided so that you will
|
|
not need to qualify the table name in SQL statements.</li>
|
|
<li id="jdbcex1__dup0025"><a name="jdbcex1__dup0025"><!-- --></a>These lines delete the table if it already exists.</li>
|
|
<li id="jdbcex1__dup0026"><a name="jdbcex1__dup0026"><!-- --></a>These lines create the table.</li>
|
|
<li id="jdbcex1__dup0027"><a name="jdbcex1__dup0027"><!-- --></a>This line prepares a statement that will insert rows into
|
|
the table. Because you will be executing this statement several times, you
|
|
should use a PreparedStatement and parameter markers.</li>
|
|
<li id="jdbcex1__dup0028"><a name="jdbcex1__dup0028"><!-- --></a>This block of code populates the table for you; every time
|
|
the loop is executed, it inserts a row into the table.</li>
|
|
<li id="jdbcex1__dup0029"><a name="jdbcex1__dup0029"><!-- --></a>Now that the table has been created and filled in, this statement
|
|
closes the connection to the database.</li>
|
|
</ol>
|
|
<p>[ <a href="jdbcex2.htm#jdbcex2">Next part</a> ]</p>
|
|
</div>
|
|
</div>
|
|
|
|
</body>
|
|
</html> |