Creating the ActiveSpaces JDBC Connection
After registering the ActiveSpaces JDBC driver, the next step is to make the ActiveSpaces JDBC connection.
The DriverManager interface supports the following methods for creating a connection:
Connection getConnection(String url); Connection getConnection(String url, Properties info); Connection getConnection(String url, String user, String password);The
url parameter is the JDBC URL to connect to a database, or in our case a data grid. Ensure that
url adheres to the format specified in
JDBC URL Format. The
info parameter is optional and can contain a set of Java properties that are required for connecting to the data grid. To understand these properties, see ActiveSpaces Properties . Note: Properties passed to the
connect()
method override the properties specified in the JDBC URL. The names of the properties follow the same rules for property names in the JDBC URL.
Before you beginFirst complete the steps listed in
Registering the ActiveSpaces JDBC Driver with the Driver Manager.
- ProcedureAfter registering the driver, create a JDBC connection to the data grid. To connect to the data grid, you can use any of the
getConnection() methods. The following code snippet uses
Connection getConnection(String url) :
// create a JDBC connection to the data grid
String jdbcURL = "jdbc:tibco:tibdg:_default;realmurl=
http://localhost:8080";
java.sql.Connection jdbcConnection =
java.sql.DriverManager.getConnection(jdbcURL);
An Example of Registering and Connecting to the Data Grid
This example shows the code snippet to be inserted to your Java code to register and connect to the data grid. The following example uses the Connection getConnection(String url, Properties info) method to create a connection.
public java.sql.Connection getConnection (Properties props) throws SQLException
{
// Register the ActiveSpaces JDBC Driver with the JDBC
DriverManager
try
{
Class.forName("com.tibco.datagrid.jdbc.DriverImpl");
}
catch (ClassNotFoundException ex)
{
// handle exception
}
// Establish a JDBC connection to the data grid
String jdbcURL = "jdbc:tibco:tibdg:_default";
Properties props = new Properties();
props.setProperty(
com.tibco.datagrid.Connection.TIBDG_CONNECTION_PROPERTY_STRING_REALMURL,
"http://localhost:8080");
return java.sql.DriverManager.getConnection(jdbcURL, props)
}
Subtopics