Connecting JdbcRowSet with a Connection URL

This section describes how to connect a JdbcRowSet object to a database server with a connection URL.

If a JdbcRowSet object is created without a connection to the database server, you can provide a connection URL string by calling the setUrl() method. A connection object will be created later by the JdbcRowSet object with DriverManager when you call the execute() method. Here is a sample sequence of method calls:

// Creating a JdbcRowSet object wihout connection
  JdbcRowSet jrs = RowSetProvider.newFactory().createJdbcRowSet();

// Provide a connection URL for future use
  jrs.setUrl("connection_url");

// Provide a SQL statement for future use
  jrs.setCommand("SQL_statement");

// Create a connnection using DriverManager
// Execute the SQL statement
  jrs.execute();

Note that the name of method setUrl() is very confusing with another method setURL(), which sets a URL value to a parameter.

Note also that the connection URL and user information can also be provided through another constructor:

  JdbcRowSet jrs = new JdbcRowSetImpl("connection_url",
    "user", "password");

To test connection method, I wrote the following tutorial program:

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

// Not needed anymore
//    Class.forName("com.mysql.jdbc.Driver") ;

// 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:mysql://localhost/HerongDB"
        +"?user=Herong&password=TopSecret");

// Set a SQL statement
      jrs.setCommand("SELECT 'Hello world!'");

// Connect and run the statement
      jrs.execute();

// Get the result
      jrs.next();
      System.out.println("Result: "+jrs.getString(1));

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

Make sure the MySQL server is running on the local machine, then run this program. The result confirms that the connection is working.

herong> javac MySqlRowSetConnectionUrl.java

herong> java -cp .:mysql-connector-java.jar MySqlRowSetConnectionUrl
Result: Hello world!

Note that if you access the com.sun.rowset.JdbcRowSetImpl class directly, "JdbcRowSet jrs = new com.sun.rowset.JdbcRowSetImpl();", you will get a warning message like this:

MySqlRowSetConnectionUrl.java:16:
warning: com.sun.rowset.JdbcRowSetImpl is Sun proprietary API and
may be removed in a future release
        = new com.sun.rowset.JdbcRowSetImpl();
                           ^
1 warning

Table of Contents

 About This Book

 JDBC (Java Database Connectivity) Introduction

 JDK (Java SE) Installation

 Installing and Running Java DB - Derby

 Derby (Java DB) JDBC Driver

 Derby (Java DB) JDBC DataSource Objects

 Java DB (Derby) - DML Statements

 Java DB (Derby) - ResultSet Objects of Queries

 Java DB (Derby) - PreparedStatement

 MySQL Installation on Windows

 MySQL JDBC Driver (MySQL Connector/J)

 MySQL - PreparedStatement

MySQL - Reference Implementation of JdbcRowSet

 Overview of RowSet Objects

 Connecting JdbcRowSet to Database Servers

 Sun Implementation of JdbcRowSet API

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

 MySQL - JBDC CallableStatement

 MySQL CLOB (Character Large Object) - TEXT

 MySQL BLOB (Binary Large Object) - BLOB

 Oracle Express Edition Installation on Windows

 Oracle JDBC Drivers

 Oracle - Reference Implementation of JdbcRowSet

 Oracle - PreparedStatement

 Oracle - JBDC CallableStatement

 Oracle CLOB (Character Large Object) - TEXT

 Oracle BLOB (Binary Large Object) - BLOB

 Microsoft SQL Server Express Edition

 Microsoft JDBC Driver for SQL Server

 Microsoft JDBC Driver - Query Statements and Result Sets

 Microsoft JDBC Driver - DatabaseMetaData Object

 Microsoft JDBC Driver - DDL Statements

 Microsoft JDBC Driver - DML Statements

 SQL Server - PreparedStatement

 SQL Server CLOB (Character Large Object) - TEXT

 SQL Server BLOB (Binary Large Object) - BLOB

 JDBC-ODBC Bridge Driver - sun.jdbc.odbc.JdbcOdbcDriver

 JDBC-ODBC Bridge Driver - Flat Text Files

 JDBC-ODBC Bridge Driver - MS Access

 JDBC-ODBC Bridge Driver - MS SQL Server

 Summary of JDBC Drivers and Database Servers

 Using Connection Pool with JDBC

 Archived Tutorials

 References

 Full Version in PDF/EPUB