Skip to main content

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 Driver classes extend java.sql.Driver.
  • The DataSource classes extend javax.sql.DataSource and javax.sql.ConnectionPoolDataSource.

The connector supports the following fully-qualified class names (FQCNs) that are independent of the JDBC version:

  • com.simba.googledatabase.jdbc.HS1Driver
  • com.simba.database.jdbc.HS2Driver
  • com.simba.googledatabase.jdbc.HS1DataSource
  • com.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.Driver
  • com.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.CloudSpanner41Driver
  • com.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.CloudSpanner42Driver
  • com.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

setCustomProperty(String key, String value)

Set a property that is not otherwise supported by one of the set/get methods listed in this table.

Parameters:

  • key: The key name of the property.
  • value: The value that you want to specify for the property.

After setting a custom property, you can access it by calling the getCustomProperty() method.

setLogDirectory(String logDirectory)

Set the directory that the connector uses for logging.

Parameters:

  • logDirectory: The directory where you want log files to be saved.

This property is equivalent to the connection-level property LogPath, which can be configured in the connection URL.

setLoginTimeout(int seconds)

Set the amount of time that the connector waits before timing out during login attempts.

Parameters:

  • seconds: The number of seconds to wait before timing out a login attempt.

This property is equivalent to the connection-level property ConnectTimeout, which can be configured in the connection URL.

setLogLevel(String level)

Set the logging level, which specifies the level of detail that the connector includes in log files.

Parameters:

  • level: The logging level to use, indicated by a number ranging from 0 to 6 (inclusive). 0 disables logging, while 6 is the most detailed logging level.

This property is equivalent to the connection-level property LogLevel, which can be configured in the connection URL.

setLogWriter(PrintWriter writer)

Set the writer that the connector uses for logging.

Parameters:

  • writer: The java.io.PrintWriter object to use for writing the log.

setPassword(String pass)

Set the password to use for authenticating the connection. Connections to Database require a secret key instead of an account password.

Parameters:

  • pass: The secret key provided by your AWS account.

This property is equivalent to the connection-level property Password, which can be configured in the connection URL.

setURL(String url)

Set the connection URL to use for connecting to Database.

Parameters:

setUserID(String id)

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:

  • id: The access key provided by your AWS account.

This property is equivalent to the connection-level property User, which can be configured in the connection URL.

Was this article helpful?

We're sorry to hear that.