JDBC for MySQL - Herong's Tutorial Examples - v3.13, by Herong Yang
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 without 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) {
String tzid = java.util.TimeZone.getDefault().getID();
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&serverTimezone="+tzid);
// 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
JDBC (Java Database Connectivity) Introduction
MySQL JDBC Driver (MySQL Connector/J)
►MySQL - Reference Implementation of JdbcRowSet
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