JdbcRowSet Query Statement with Parameters

This section describes how to define parameters in the query statement of a JdbcRowSet object.

When you call the setCommand() method to provide the SELECT query statement, you can define some parameters in the same way as a PreparedStatement. Here is a sample program showing you set parameters and provide parameter values:

/* OracleRowSetParameter.java
 * Copyright (c) HerongYang.com. All Rights Reserved.
 */
import java.sql.*;
import javax.sql.rowset.RowSetProvider;
import javax.sql.rowset.JdbcRowSet;
public class OracleRowSetParameter {
  public static void main(String [] args) {
    try {

// RowSetFactory should be used now
//      JdbcRowSet jrs
//        = new com.sun.rowset.JdbcRowSetImpl();
      JdbcRowSet jrs
        = RowSetProvider.newFactory().createJdbcRowSet();

// Set the connection URL for the DriverManager
      jrs.setUrl("jdbc:oracle:thin:Herong/TopSecret"
        +"@//localhost:1521/XE");

// Set a SQL statement with a parameter
      jrs.setCommand("SELECT * FROM Profile WHERE ID = ?");

// Set parameter values
      jrs.setInt(1, 1);

// Connect and run the statement
      jrs.execute();
      jrs.next();
      System.out.println("User #1: "
        +jrs.getString("FirstName")+" "+jrs.getString("LastName"));

// Repeating for another parameter value
      jrs.setInt(1, 12);
      jrs.execute();
      jrs.next();
      System.out.println("User #12: "
        +jrs.getString("FirstName")+" "+jrs.getString("LastName"));

// Close resource
      jrs.close();
    } catch (Exception e) {
      System.err.println("Exception: "+e.getMessage());
    }
  }
}

The parameter in this program represents the ID value in the Profile table. You may need to change the parameter values if your Profile table has different ID values. Here is the output of the program:

herong> java -cp .;ojdbc11.jar OracleRowSetParameter

User #1: Herong null
User #12: bd 346ac

Table of Contents

 About This Book

 JDBC (Java Database Connectivity) Introduction

 JDK (Java SE) Installation

 Oracle Express Edition Installation on Windows

 Oracle JDBC Drivers

Oracle - Reference Implementation of JdbcRowSet

 Overview of RowSet Objects

 Installation of JdbcRowSet Reference Implementation

 Connecting JdbcRowSet to Database Servers

 Connecting JdbcRowSet with a Connection URL

 Connecting JdbcRowSet with a Predefined Connection Object

 Connecting JdbcRowSet with a Predefined ResultSet Object

 Connecting JdbcRowSet with JNDI Directory Service

JdbcRowSet Query Statement with Parameters

 Inserting Rows with JdbcRowSet Objects

 Oracle - PreparedStatement

 Oracle - JBDC CallableStatement

 Oracle CLOB (Character Large Object) - TEXT

 Oracle BLOB (Binary Large Object) - BLOB

 Archived Tutorials

 References

 Full Version in PDF/EPUB