OceanBase Connector/J enables you to establish a database connection by using DriverManager or a connection pool.
Establish a database connection by using DriverManager
We recommend that you use the DriverManager class to connect to OceanBase Connector/J.
When you use the DriverManager class, set the parameters as follows to establish a connection to OceanBase Database:
Connection connection = DriverManager.getConnection("jdbc:oceanbase://host:port/user=root&password=***");
Before you use DriverManager, you need to use Class.forName to load the class, and then use the IP address and port number of OceanBase Database to connect to the database. To be specific, enter the corresponding IP address, port number, and schema name in String url.
The complete sample code for establishing a database connection is as follows:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class HelloWorld {
public static void main(String[] args) {
try {
String url = "jdbc:oceanbase://ipaddress:port/shemaname?pool=false";
String user = "username";
String password = "***";
Class.forName("com.oceanbase.jdbc.Driver");
Connection connection = DriverManager.getConnection(url, user, password);
} catch (Throwable e) {
e.printStackTrace();
}
}
Note
In Apsara Stack or a self-managed environment, you must specify the username field in the format of String user = "username@tenant name#cluster name";.
After the database connection is established, you can perform steps in the following example to load the class.
javac -cp target/oceanbase-client-{version}.jar HelloWorld.java
java -cp .:target/oceanbase-client-{version}.jar HelloWorld
Establish a database connection by using a connection pool
You can also connect to OceanBase Connector/J by using a connection pool.
OceanBase Connector/J supports two datasource pool implementations:
OceanbaseDataSource: the basic implementation. It establishes a new connection each time thegetConnection()method is called.OceanbasePoolDataSource: a connection pool implementation. It maintains a connection pool, and when a new connection is requested, a connection is borrowed from the pool.
Internal pool
The internal pool of OceanBase Connector/J provides a very fast pool implementation and solves the following issues found in most Java pools:
Two different connection state cleanup modes are available after a connection is released.
Inactive connections are automatically released. In a Java pool, connections are automatically released to avoid problems that may occur when the server closes connections after the
@wait_timeoutthreshold is reached.
External pool
You must configure the com.oceanbase.jdbc.Driver class when you use an external connection pool.
Example: Use the HikariCP JDBC connection pool.
final HikariDataSource ds = new HikariDataSource();
ds.setMaximumPoolSize(10);
ds.setDriverClassName("com.oceanbase.jdbc.Driver");
ds.setJdbcUrl("jdbc:oceanbase://localhost:2883/db");
ds.addDataSourceProperty("user", "root");
ds.addDataSourceProperty("password", "OBClient");
ds.setAutoCommit(false);