java06_main.java

TBD: Update GenDefines.txt with a $DESCRIPTION for this example.

import java.sql.*;
public class java06_main {
/*
* EXAMPLE - java06 for JDBC
* When you run this example, two databases are created each with a table.
* Each table is populated with a few rows and finally the data read with
* a single query.
*
* Full documentation for RDM is available at http://docs.raima.com/rdm/
*
* For simplicity this example utilizes basic exception handling
* but good programming practices would dictate that this occur in
* a full application.
*/
public static void main(String[] args) throws SQLException {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try
{
CreateDatabases();
/* Create the connection with a simple connection string */
conn = DriverManager.getConnection("jdbc:raima:rdm://local");
conn.setAutoCommit(true);
//CreateDatabases(conn);
/* Open the connection to the database */
stmt = conn.createStatement();
stmt.execute("USE java06_1");
/* Insert one row into the table in the first database */
stmt.executeUpdate("INSERT INTO Table1 VALUES (1, 'database1 1')");
conn.commit();
stmt.execute("USE java06_2");
/* Insert three rows into the table in the second database */
for (int ii = 1; ii < 4; ii++)
{
stmt.executeUpdate("INSERT INTO Table2 VALUES (1, 'database2 " + ii + "')");
}
/* Scan through the data, generating the result */
rs = stmt.executeQuery("SELECT Table1.str, Table2.str FROM Table1, Table2 WHERE Table1.id = Table2.id"); while (rs.next() != false)
{
System.out.println(rs.getString(1) + " -- " + rs.getString(2));
}
}
catch (SQLException sqle)
{
sqle.printStackTrace();
}
finally
{
if(rs != null)
rs.close();
if(stmt != null)
stmt.close();
if(conn != null)
conn.close();
}
}
private static void CreateDatabases()
{
Connection conn = null;
try {
conn = DriverManager.getConnection("jdbc:raima:rdm://local");
/* Create the example databases */
String query1 = "CREATE TABLE Table1 (id INTEGER PRIMARY KEY, str CHAR(30))";
String query2 = "CREATE TABLE Table2 (id INTEGER, str CHAR(30))";
/* Drop any existing copies of the database */
DropDatabase(conn, "java06_1");
DropDatabase(conn, "java06_2");
/* Attempt to create the databases */
CreateThisDatabase(conn, "java06_1", query1);
CreateThisDatabase(conn, "java06_2", query2);
conn.close();
}
catch (SQLException sqle)
{
sqle.printStackTrace();
}
}
private static void CreateThisDatabase(Connection conn, String dbName, String query)
{
Statement stmt = null;
/* Create the specified database. Make sure the database is
* closed when we are done so other databases can be created
* after this if necessary. */
try
{
stmt = conn.createStatement();
stmt.execute("CREATE DATABASE " + dbName);
stmt.execute(query);
conn.commit();
}
catch(SQLException sqle)
{
sqle.printStackTrace();
}
}
private static void DropDatabase(Connection conn, String dbName)
{
Statement stmt = null;
/* Attempt to drop this database. If the database does not
* already exist this will fail and we simply ignore the
* failure for this sample. */
try
{
stmt = conn.createStatement();
stmt.executeUpdate("DROP DATABASE " + dbName);
}
catch (SQLException sqle)
{
sqle.printStackTrace();
}
}
}