本节主要介绍如何使用 TDSQL PG JDBC 连接 TDSQL PG 数据库,实现数据增删查改等基本操作。
简介
The Java Database Connectivity (JDBC) API 是 Java 编程语言与各种数据库、SQL 数据库和其他表格数据源(如电子表格或平面文件)之间独立于数据库的连接的行业标准。JDBC API 提供了基于 SQL 数据库访问的调用级 API。TDSQL PG Connector 驱动程序是腾讯云独立更新和维护的、商业化的、纯 Java 编写的 JDBC 驱动程序;使用 PostgreSQL 原生网络协议进行通信,并特别针对 TDSQL PG 数据库的使用语法和功能特性进行了开发和适配。
连接数据库
准备 TDSQL PG JDBC 驱动包
tdsql-pg-connector-java8-x.x.x.jar
对应 JDK8 及以上tdsql-pg-connector-java7-x.x.x.jar
对应 JDK7tdsql-pg-connector-java6-x.x.x.jar
对应 JDK6配置包名和连接串
TDSQL PG JDBC 驱动名为
com.tencentcloud.tdsql.pg.jdbc.Driver
。用于连接 TDSQL PG 服务器的 JDBC URL 的一般格式如下,方括号([ ])中的选项为可选内容:jdbc:tdsql-pg:[//host[:port]/][database][?property1=value1[&property2=value2]...]
,其中:jdbc:tdsql-pg: (固定):TDSQL PG Connector 驱动程序固定使用的 URL 前缀。
host (可选):服务器地址。可以是 DNS 或 IP 地址,也可以是本地计算机的 localhost 或127.0.0.1。默认值为 localhost。
port (可选):服务器上监听的端口号。默认值为5432。
database (可选):数据库名。默认为连接中使用的用户名。
propertyX (可选):是一个或多个连接参数。
说明:
连接示例
示例程序:
testConnect.java
import java.sql.Connection;import java.sql.DriverManager;public class testConnect {public static void main(String args[]) {try {Class.forName("com.tencentcloud.tdsql.pg.jdbc.Driver");// host、port、database、user、password 需要替换为数据库管理员提供的数据库相关信息Connection conn = DriverManager.getConnection("jdbc:tdsql-pg://host:port/database?oracle_compile=true","user", "password");if (conn != null) {System.out.println("连接数据库成功!");conn.close();}} catch (Exception e) {e.printStackTrace();}}}
注意:
host
、port
、database
、user
、password
需要改为实际使用的参数。运行结果
连接数据库成功!
开发示例
基础增删查改示例
示例程序:
testInsert.java
import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.Statement;public class testInsert {public static void main(String args[]) {try {Class.forName("com.tencentcloud.tdsql.pg.jdbc.Driver");// host、port、database、user、password 需要替换为数据库管理员提供的数据库相关信息Connection conn = DriverManager.getConnection("jdbc:tdsql-pg://host:port/database?oracle_compile=true","user", "password");Statement stmt = conn.createStatement();// 创建表System.out.println("------创建表------");stmt.execute("drop table if exists studentTab");stmt.execute("create table studentTab(id int, name varchar)");System.out.println("创建studentTab表");// 插入数据System.out.println("------插入数据------");stmt.execute("insert into studentTab values (1,'Alex'),(2,'Bob'),(3,'John')");System.out.println("插入3条数据");// 查询数据System.out.println("------查询数据------");ResultSet rs1 = stmt.executeQuery("select * from studentTab order by id");while (rs1.next()) {System.out.println(rs1.getInt(1) + " " + rs1.getString(2));}// 更改数据System.out.println("------更改数据------");stmt.execute("update studentTab set name = 'Alice' where id = 1");System.out.println("更改id为1的数据");// 查询数据System.out.println("------查询数据------");ResultSet rs2 = stmt.executeQuery("select * from studentTab order by id");while (rs2.next()) {System.out.println(rs2.getInt(1) + " " + rs2.getString(2));}// 删除数据System.out.println("------删除数据------");stmt.execute("delete from studentTab where id = 2");System.out.println("删除id为2的数据");// 查询数据System.out.println("------查询数据------");ResultSet rs3 = stmt.executeQuery("select * from studentTab order by id");while (rs3.next()) {System.out.println(rs3.getInt(1) + " " + rs3.getString(2));}// 关闭对象rs1.close();rs2.close();rs3.close();stmt.close();conn.close();} catch (Exception e) {e.printStackTrace();}}}
运行结果
------创建表------创建studentTab表------插入数据------插入3条数据------查询数据------1 Alex2 Bob3 John------更改数据------更改id为1的数据------查询数据------1 Alice2 Bob3 John------删除数据------删除id为2的数据------查询数据------1 Alice3 John
prepare 语法示例
示例程序:
testPrepare.java
import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.Statement;public class testPrepare {public static void main(String args[]) {try {Class.forName("com.tencentcloud.tdsql.pg.jdbc.Driver");// host、port、database、user、password 需要替换为数据库管理员提供的数据库相关信息Connection conn = DriverManager.getConnection("jdbc:tdsql-pg://host:port/database?oracle_compile=true","user", "password");Statement stmt = conn.createStatement();// 复用增删查改示例中的studentTab表// prepare插入数据System.out.println("------prepare插入数据------");PreparedStatement pstmt = conn.prepareStatement("insert into studentTab values (?, ?)");pstmt.setInt(1, 4);pstmt.setString(2, "Luna");pstmt.execute();System.out.println("插入1条数据");// 查询数据System.out.println("------查询数据------");ResultSet rs = stmt.executeQuery("select * from studentTab order by id");while (rs.next()) {System.out.println(rs.getInt(1) + " " + rs.getString(2));}// 关闭对象rs.close();pstmt.close();stmt.close();conn.close();} catch (Exception e) {e.printStackTrace();}}}
注意:
host
、port
、database
、user
、password
需要改为实际使用的参数。运行结果
------prepare插入数据------插入1条数据------查询数据------1 Alice3 John4 Luna
调用存储过程和函数示例
示例程序:
testCall.java
import java.sql.CallableStatement;import java.sql.Connection;import java.sql.DriverManager;import java.sql.Statement;import java.sql.Types;public class testCall {public static void main(String args[]) {try {Class.forName("com.tencentcloud.tdsql.pg.jdbc.Driver");// host、port、database、user、password 需要替换为数据库管理员提供的数据库相关信息// 需要添加escapeSyntaxCallMode=callIfNoReturn参数,以确保驱动程序正确调用存储过程或者函数Connection conn = DriverManager.getConnection("jdbc:tdsql-pg://host:port/database?oracle_compile=true&escapeSyntaxCallMode=callIfNoReturn","user", "password");Statement stmt = conn.createStatement();// 创建存储过程stmt.execute("create or replace procedure testProc(p1 in int, p2 out int) is\\n"+ "begin\\n"+ " p2 := p1;\\n"+ "end;");// 调用存储过程System.out.println("------调用存储过程------");CallableStatement proc = conn.prepareCall("{call testProc(?, ?)}");proc.setInt(1, 100);proc.registerOutParameter(2, Types.INTEGER);proc.execute();System.out.println("调用结果: " + proc.getInt(2));proc.close();// 创建函数stmt.execute("create or replace function testFunc(p1 varchar) return varchar\\n"+ "is\\n"+ "begin\\n"+ " return p1;\\n"+ "end;");// 调用函数System.out.println("------调用函数------");CallableStatement func = conn.prepareCall("{? = call testFunc(?)}");func.registerOutParameter(1, Types.VARCHAR);func.setString(2, "test");func.execute();System.out.println("调用结果: " + func.getString(1));func.close();// 关闭对象stmt.close();conn.close();} catch (Exception e) {e.printStackTrace();}}}
注意:
host
、port
、database
、user
、password
需要改为实际使用的参数。
需要添加 escapeSyntaxCallMode=callIfNoReturn 参数,以确保驱动程序正确调用存储过程或者函数。运行结果
------调用存储过程------调用结果: 100------调用函数------调用结果: test