本文档介绍如何快速使用应用访问已部署的 OceanBase 数据库。
Java 应用连接 OceanBase 数据库推荐使用 mysql-connecter 版本为 5.1.40。关于 JDBC 方法详情请参考 JDBC 相关手册。
如下为连接应用测试租户 wh_tenant1 的 JDBC 代码样例。
import java.sql.*;
public class SelectDemo {
public static void select() throws SQLException{
String ip = "192.168.1.100";
String port = "2883";
String database = "wh_db1";
String url = String.format("jdbc:mysql://%s:%s/%s?rewriteBatchedStatements=true", ip, port, database);
String username = "root@wh_tenant1#obtest";
String password = "";
String selectSql = "select concat(replace(GROUP_CONCAT(col2),',',' '),'!') as OceanBase from wh_tab2 where col1 in (3,4);
";
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url, username, password);
PreparedStatement ps = conn.prepareStatement(selectSql);
ResultSet rs = ps.executeQuery();
while (rs.next()){
System.out.println(String.format("### %s ###", rs.getString("OceanBase")));
}
ps.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != conn) {
conn.close();
}
}
}
public static void main(String[] args) {
try {
select();
} catch (SQLException e) {
e.printStackTrace();
}
}
}