This topic describes how to connect Java applications to OceanBase Database.
Prerequisites
The basic database development environment is set.
Java Development Kit (JDK) 8 is installed.
The installation package of OceanBase Connector/J is obtained from OceanBase Technical Support.
Procedure
Place the JAR installation package of OceanBase Connector/J in the local directory by using cmd and perform the following connection test:
Compile the Java file
HelloWorld.javaand load OceanBase Connector/J.public class HelloWorld { public static void main(String[] args) { try { Class.forName("com.oceanbase.jdbc.Driver"); } catch (ClassNotFoundException e) { e.printStackTrace(); } } }Run the following command to compile the Java file:
javac -cp target/oceanbase-client-{version}.jar HelloWorld.javaNote
You can modify the compilation path.
Run the following command to run the Java file:
java -cp .:target/oceanbase-client-{version}.jar HelloWorldIf steps 2 and 3 are completed with no error message returned, the JAR package of OceanBase Connector/J is correctly loaded.
Connect to OceanBase Database.
After OceanBase Connector/J is loaded, connect to OceanBase Database based on the IP address and port. Enter the actual IP address, port number, and schema name in the
String urlfield in the following code: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 = "password"; Class.forName("com.oceanbase.jdbc.Driver"); Connection connection = DriverManager.getConnection(url, user, password); } catch (ClassNotFoundException e) { e.printStackTrace(); } }After the connection to OceanBase Database is established, repeat steps 2 and 3 to load classes.
For more information about OceanBase Connector/J, see OceanBase Connector/J.