Registering the Connector Class
Before connecting to your data, you must register the appropriate class for your application.
The following classes are used to connect the Product to Database data storesthe Database service:
- The
Driverclasses extendjava.sql.Driver. - The
DataSourceclasses extendjavax.sql.DataSourceandjavax.sql.ConnectionPoolDataSource.
The connector supports the following fully-qualified class names (FQCNs) that are independent of the JDBC version:
com.simba.googledatabase.jdbc.HS1Drivercom.simba.database.jdbc.HS2Drivercom.simba.googledatabase.jdbc.HS1DataSourcecom.simba.database.jdbc.HS2DataSource
The connector supports the following fully-qualified class names (FQCNs) that are independent of the JDBC version:
-
com.simba..jdbc.Driver -
com.simba..jdbc.DataSource
To support JDBC 4.1, classes with the following fully-qualified class names (FQCNs) are available:
-
com.simba..jdbc41.Driver -
com.simba..jdbc41.DataSource
To support JDBC 4.2, classes with the following fully-qualified class names (FQCNs) are available:
-
com.simba..jdbc42.Driver -
com.simba..jdbc42.DataSource
To support JDBC 4.0, classes with the following fully-qualified class names (FQCNs) are available:
com.simba.database.jdbc4.Drivercom.simba.database.jdbc4.DataSource
Note:
Support for JDBC 4.0 and 4.1 are deprecated, and will be removed in a future release of this connector. For more information, see the release notes.
To support JDBC 4.1, classes with the following fully-qualified class names (FQCNs)FQCNs are available:
com.simba.database.core.jdbc41.CloudSpanner41Drivercom.simba.database.core.jdbc41.CloudSpanner41DataSource
To support JDBC 4.2, classes with the following fully-qualified class names (FQCNs)FQCNs are available:
com.simba.database.core.jdbc42.CloudSpanner42Drivercom.simba.database.core.jdbc42.CloudSpanner42DataSource
The following sample code shows how to use the DriverManager class to establish a connection for JDBC 4.0 4.2:
Note:
In these examples, the line Class.forName(DRIVER_CLASS); is only required for JDBC 4.0.
private static Connection connectViaDM() throws Exception
{
Connection connection = null;
Class.forName(DRIVER_CLASS);
connection = DriverManager.getConnection(CONNECTION_URL);
return connection;
}
The following sample code shows how to use the DataSource class to establish a connection:
private static Connection connectViaDS() throws Exception
{
Connection connection = null;
Class.forName(DRIVER_CLASS);
DataSource ds = new com.simba.database.jdbc42.DataSource();
DataSource ds = new com.simba.googledatabase.jdbc.DataSource();
DataSource ds = new com.simba.database.core.jdbc42.CloudSpanner42DataSource();
DataSource ds = new com.simba.database.jdbc.HS1DataSource();
DataSource ds = new com.simba..jdbc.DataSource();
ds.setURL(CONNECTION_URL);
connection = ds.getConnection();
return connection;
}
The following sample code shows how to use service authentication to establish a connection:
private static Connection connectViaDS() throws Exception
{
Connection connection = null;
Class.forName(DRIVER_CLASS);
DataSource ds = new
com.simba.database.jdbc.DataSource();
ds.setURL(CONNECTION_URL);
ds.setProjectId(PROJECT);
ds.setOAuthType(0); // Service Authentication
ds.setOAuthServiceAcctEmail(EMAIL);
ds.setOAuthPvtKeyFile(KEYFILE);
connection = ds.getConnection();
return connection;
}
Note:
When using the DataSource class to establish a connection, all the required properties of CONNECTION_URL should be configured. For service authentication, the properties can be configured separately.
When using the DataSource class to connect to Database, you can call the following methods to configure DataSource properties:
| Method | Description |
|---|---|
|
Set a property that is not otherwise supported by one of the set/get methods listed in this table. Parameters:
After setting a custom property, you can access it by calling the |
|
Set the directory that the connector uses for logging. Parameters:
This property is equivalent to the connection-level property LogPath, which can be configured in the connection URL. |
|
Set the amount of time that the connector waits before timing out during login attempts. Parameters:
This property is equivalent to the connection-level property ConnectTimeout, which can be configured in the connection URL. |
|
Set the logging level, which specifies the level of detail that the connector includes in log files. Parameters:
This property is equivalent to the connection-level property LogLevel, which can be configured in the connection URL. |
|
Set the writer that the connector uses for logging. Parameters:
|
|
Set the password to use for authenticating the connection. Connections to Database require a secret key instead of an account password. Parameters:
This property is equivalent to the connection-level property Password, which can be configured in the connection URL. |
|
Set the connection URL to use for connecting to Database. Parameters:
|
|
Set the user ID to use for authenticating the connection. Connections to Database require an access key instead of the ID of a user account. Parameters:
This property is equivalent to the connection-level property User, which can be configured in the connection URL. |
- Installing and Using the Product
- System Requirements
- System Requirements
- Referencing the JDBC Connector Libraries
- Building the Connection URL
- Building the Connection URL
- Building the Connection URL on page 1
- Building the Connection URL on page 1
- Building the Connection URL on page 1
- Building the Connection URL on page 1
- Connecting to the Data Source on page 1
- Building the Connection URL on page 1
- Building the Connection URL on page 1
- Building the Connection URL on page 1
- Building the Connection URL on page 1
- Building the Connection URL on page 1
- Building the Connection URL on page 1
- Building the Connection URL on page 1
- Configuring the Product on page 1