Basic JDBC Application Steps

There are several steps you need to take in order to create a Java program using the JDBC API. JDBC is the Java extension of the SQL API and will show some similarities in program layout (Such as creating a handle, preparing and then executing each statements. Although these are referenced as objects or classes in Java). Refer to the example HelloWorldJDBC for reference. Some code snippets will be provided.

  1. Set up and initialize your application's use of the JDBC SQL API as follows.
    1. Using the DriverManager class, create your connection using the Raima connection URL and the following DriverManager method:

      Conn = DriverManager.getConnection("jdbc:raima:rdm://local");

      The string passed to the getConnection() method is a URL which designates which driver to use, how to get to the database, what database to connect to, and connection parameters. See RDMDriver.connect() for more information.

      *Note for better de-allocation practices, create a new try block after each object has been allocated. See full code for clarification.
    2. Create your Statement object by calling the following Connection method:

      Connection Conn = DriverManager.getConnection("jdbc:raima:rdm://local/<databasename>");
      Statement Stmt = Conn.createStatement();
  2. Next create a Statement, PreparedStatement, or CallableStatement. If you would like to use parametrized statements, use a PreparedStatement. A CallableStatement adds the ability to use named parameters.

    1. Create all needed PreparedStatement objects or a CallableStatement objects to prepare your application for execution by calling methods such as:

      PreparedStatement prepStmt = 
          Conn.prepareStatement("INSERT INTO hello_table (f00) VALUES (?)");

      or

      CallableStatement callStmt = 
          Conn.prepareCall("INSERT INTO hello_table (f00) VALUES ( :infoName )");
    2. Call the setter methods to bind the parameters in your prepared statements, such as:

      prepStmt.setString(1, "Hello World!");

      or

      callStmt.setString("infoName", "Hello World!");
  3. Once you have all the required parameters supplied call execute() to execute you a prepared (or callable) statement. If no parameters are needed a standard statement can be used.

    1. Call prepStmt.execute() method to execute all of your prepared statements. You can also use your Statement object to execute statements with no preparation. This was used above when you opened the database.

      ResultSet rs = Stmt.execute("SELECT * FROM hello_table");

      or

      ResultSet rs = prepStmt.execute();

      or

      ResultSet rs = callStmt.execute();
    2. Create a while loop checking for the next value in your result set to be null or your ResultSet.next() method returns false meaning no more data. This will traverse through your table after a SELECT statement has been performed and stored into a ResultSet. Inside of your loop call a getter method such as ResultSet.getString(1) to receive the data from your table in the current row.

      while (rs.next())
      {
          String name = rs.getString(1); // retrieve the first column as a string.
          ...
      }
      
  4. When your application is ready to terminate there are several steps you must take to insure nothing is left behind.
    1. For every nested try block, you must have a finally block that closes the corresponding Object by performing the Class.close() method. For example :

      ...
      } finally {
      rs.close();
      }

      See complete code below for a more complete example

    2. The last try block must include a catch block to handle the SQLExceptions that could be thrown. For example:

      ...
      } catch (SQLException exception) {
          System.err.println("SQLException : "
              + exception.toString()); // Displays error to standard out
      }
      

Hello World!

If the steps above are followed you should be able to access a simple database, store in some data and read the data back out. You can see an example of this in the common "Hello World!" example given below. The database schema for this sample is helloWorld.sdl.

(missing or bad snippet)