java02_main.java
                                            
                                            Primary and Foreign-key reference. Two table types, second referencing the first.
import java.sql.*;
                                                    public class java02_main {
                                                    /*
                                                    
                                                         * EXAMPLE SQL02 for JDBC
                                                    
                                                         * When you run this example, a new database is created with two tables
                                                    
                                                         * where one references the other.  Then a single row is created in the 
                                                    
                                                         * referenced table and four rows are created in the referencing table.
                                                    
                                                         * Finally all the data is read from the database using a join.
                                                    
                                                         * 
                                                    
                                                         * 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
                                                    
                                                            {
                                                    /* Create the connection with a simple connection string */
                                                    
                                                                conn = DriverManager.getConnection("jdbc:raima:rdm://local");
                                                                conn.setAutoCommit(true);
                                                                CreateDatabase(conn);
                                                    /* Open the connection to the database */
                                                    
                                                                stmt = conn.createStatement();
                                                    /* Insert a row into the referenced table */
                                                    
                                                                stmt.executeUpdate("INSERT INTO PriTab Values 1,'referenced 1'");
                                                    /* Insert four rows into the referenced table */
                                                    
                                                    for (int ii = 1; ii < 5; ii++)
                                                                {
                                                                    stmt.executeUpdate("INSERT INTO ForTab VALUES 'referencing " + ii + "', 1");
                                                                }
                                                    /* Scan through all the existing rows using a join and display the contents */
                                                    
                                                                rs = stmt.executeQuery("SELECT pstr, fstr FROM ForTab, PriTab where ForTab.pid = PriTab.pid");
                                                    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 CreateDatabase(Connection conn)
                                                        {
                                                            Statement stmt = null;
                                                    try
                                                    
                                                            {
                                                    /* Create the example database */
                                                    
                                                                stmt = conn.createStatement();
                                                    /* Since the database is created here, it cannot be created twice */
                                                    
                                                    try {
                                                                    stmt.executeUpdate("DROP DATABASE java02");
                                                                } catch (SQLException sqle) {}
                                                                stmt.execute("CREATE DATABASE java02");
                                                                stmt.execute("CREATE TABLE PriTab (pid INTEGER PRIMARY KEY, pstr CHAR(30))");
                                                                stmt.execute("CREATE TABLE ForTab (fstr CHAR(30), pid INTEGER REFERENCES PriTab(pid))");
                                                                conn.commit();
                                                                stmt.close();
                                                                stmt = null;
                                                            }
                                                    catch (SQLException sqle)
                                                            {
                                                                sqle.printStackTrace();
                                                            }
                                                        }       
                                                    }