Products Support Documentation Download
Basic JDBC JAVA Application Steps

A JDBC JAVA application program consists of a set of calls to the JDBC API functions in a particular sequence as outlined below. The database schema for this sample is HelloWorldJDBC.sdl.

Set up and initialize your application's use of the JDBC API as follows.

  • Create the database using the helloWorldJDBC.sdl schema if it does not already exist.
    1. rdm-create helloWorldJDBC.sdl
  • Create a String object to define your connection string in the following format: "jdbc:raima:rdm://local/HelloWorldJDBC"
    1. Create an Connection object to handle your connection to the Raima database system and pass in your connection string to the constructor.
    2. Create an Statement object to handle your SQL commands you wish to use to interact with the RDM database system.
    3. Create an ResultSet object to handle reading out data from the RDM database system.
    4. Create an RdmDataReader object to handle reading out data from the RDM database syse.
  • At this point your application is execution ready. That means that your application will ...
    1. Call executeQuery on the Statement object to execute the appropriate statements that implement the database access needs for each particular function. Alternatively you can call executeUpdate to execute an insert/update statement.
    2. Call transaction methods on the Connection object by issuing commit or rollback methods in order to commit the transaction or rollback a transaction. Savepoint objects can be used to create a savepoint on a Connection object.
    3. You iterate over the results using the ResultSet object.
  • When your application is ready to terminate you need to...
    1. Call Close on the ResultSet object you used if you read from one.
    2. Call Close on the Statement object to close the statement to your database.
    3. Call Close on the Connection object to close the connection to your database.
hello_worldJDBC.sdl
1 --
2 -- Use following command to compile:
3 -- rdm-create hello_worldJDBC.sdl
4 --
5 -- Please refer to the Database Design section in the User Manual for details of
6 -- RDM Database schema.
7 
8 create table hello_table
9 (
10  f00 char(31)
11 );

If you follow the basic procedure listed above you can easily create a simple database where you insert some data and the retrieve the data to be displayed back out. This can be seen with the common Hello World example.

In the example below return codes are mostly ignored for simplicity. This will be OK because we know what the expected results are.

1 import java.sql.Connection;
2 import java.sql.DriverManager;
3 import java.sql.PreparedStatement;
4 import java.sql.ResultSet;
5 import java.sql.SQLException;
6 import java.sql.Statement;
7 
19 public class HelloWorld_JDBCTutorial_main {
20 
30  public static void main(String[] args) throws SQLException {
31  // Connection object used for connecting to driver
32  Connection Conn = null;
33 
34  // Statement object used for executing SQL Statements
35  Statement Stmt = null;
36 
37  // Prepared Statement object used for preparing SQL Statements
38  PreparedStatement prepStmt = null;
39 
40  // Result Set object used for storing queried values
41  ResultSet rs = null;
42 
43  // The try block contains the main portion of the code. The code is contained
44  // inside here so that if it fails the exception will be caught inside the
45  // catch blocks.
46  try
47  {
48  // Connects to driver using connection URL and passed into Connection
49  // Object
50  Conn = DriverManager.getConnection("jdbc:raima:rdm://local");
51 
52  // Creates the Statement object to be used for future SQL Statement
53  // executions
54  Stmt = Conn.createStatement();
55 
56  // Opens database
57  Stmt.executeUpdate("USE hello_worldJDBC");
58 
59  //Used to clear table if there is previous data
60  Stmt.executeUpdate("DELETE FROM hello_table");
61 
62  // Prepares the insert statement for execution
63  prepStmt = Conn.prepareStatement("INSERT INTO hello_table (f00) VALUES (?)");
64 
65  // Performs a setter method for a string setting the value of the
66  // parameter above
67  prepStmt.setString(1, "Hello World!");
68 
69  //Executes prepared Statement
70  prepStmt.execute();
71 
72  // Commits all changes
73  Conn.commit();
74 
75  // Executes a select statement and stores queried results in the result
76  // set
77  rs = Stmt.executeQuery("SELECT * FROM hello_table");
78 
79  // Loops through the result set, checking for when the next value is
80  // null. We only expect 1 iteration. The contents of helloString are
81  // then received from the getter method. The results are then printed
82  // to the screen
83  while (rs.next() != false)
84  {
85  String helloString = rs.getString(1);
86  System.out.println(helloString);
87  }
88  }
89 
90  // The catch block is used to catch an exceptions that are thrown above and
91  // display them to the screen. Currently the only type of exception we can
92  // expect would be a SQLException otherwise we would have to have another
93  // catch block for each possible exception
94  catch (SQLException exception)
95  {
96  System.err.println("An exception has occured : " + exception.toString());
97  System.exit (1);
98  }
99 
100  // The finally block is used to execute after the try block has been
101  // performed. It is also executed if an exception is thrown, this way you
102  // can clean up any open objects without missing the .close() on a failure.
103  // Every object must be closed as you would a handle in ODBC.
104  finally
105  {
106  if (rs != null)
107  {
108  rs.close();
109  }
110 
111  if (prepStmt != null)
112  {
113  prepStmt.close();
114  }
115 
116  if (Stmt != null)
117  {
118  Stmt.close();
119  }
120 
121  if (Conn != null)
122  {
123  Conn.close();
124  }
125  }
126  }
127 }
128