This topic provides an example of using OceanBase Connector/J to connect to OceanBase Database.
Note
You must set the prefix of the connection string to jdbc:oceanbase. Settings of other parameters of the connection string are the same as those in native MySQL.
import java.sql.*;
public class helloworld{
public static void main(String[] args) throws SQLException{
String url = "jdbc:oceanbase://10.XXX.XXX.XXX:2882/SYS";
String username = "sys@testuser001";
String password = "******";
Connection conn = null;
try {
Class.forName("com.oceanbase.jdbc.Driver");
conn = DriverManager.getConnection(url, username, password);
PreparedStatement ps = conn.prepareStatement("select to_char(sysdate,'yyyy-MM-dd HH24:mi:ss') from dual;");
ResultSet rs = ps.executeQuery();
rs.next();
System.out.println("sysdate is:" + rs.getString(1));
rs.close();
ps.close();
} catch (Throwable e) {
e.printStackTrace();
} finally {
if (null != conn) {
conn.close();
}
}
}
}