This program uses the IBM® Toolbox for Java™ JDBC driver to create and populate a table.
[ Next part ]
Use the following as an example for your program.
//////////////////////////////////////////////////////////////////////////////////
//
// 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()); Note 1
connection = DriverManager.getConnection ("jdbc:as400://"
+ system + "/" + collectionName); Note 2
try {
Statement dropTable = connection.createStatement ();
dropTable.executeUpdate ("DROP TABLE " + tableName); Note 3
}
catch (SQLException e) {
}
Statement createTable = connection.createStatement ();
createTable.executeUpdate ("CREATE TABLE " + tableName
+ " (I INTEGER, WORD VARCHAR(20), SQUARE INTEGER, "
+ " SQUAREROOT DOUBLE)"); Note 4
PreparedStatement insert = connection.prepareStatement ("INSERT INTO "
+ tableName + " (I, WORD, SQUARE, SQUAREROOT) "
+ " VALUES (?, ?, ?, ?)"); Note 5
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 (); Note 6
}
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 (); Note 7
}
catch (SQLException e) {
// Ignore.
}
}
System.exit (0);
}
}
[ Next part ]