基础集团网站建设,app开发公司排名,网络管理系统页面,龙游住房和城乡建设局网站文章目录 openGauss学习笔记-121 openGauss 数据库管理-设置密态等值查询-使用JDBC操作密态数据库121.1 连接密态数据库121.2 调用isValid方法刷新缓存示例121.3 执行密态等值查询相关的创建密钥语句121.4 执行密态等值查询相关的创建加密表的语句121.5 执行加密表的预编译SQL语… 文章目录 openGauss学习笔记-121 openGauss 数据库管理-设置密态等值查询-使用JDBC操作密态数据库121.1 连接密态数据库121.2 调用isValid方法刷新缓存示例121.3 执行密态等值查询相关的创建密钥语句121.4 执行密态等值查询相关的创建加密表的语句121.5 执行加密表的预编译SQL语句121.6 执行加密表的批处理操作 openGauss学习笔记-121 openGauss 数据库管理-设置密态等值查询-使用JDBC操作密态数据库
121.1 连接密态数据库
连接密态数据库需要使用驱动包gsjdbc4.jar具体JDBC连接参数参考基于JDBC开发章节介绍。JDBC支持密态数据库相关操作需要设置enable_ce1示例如下。
public static Connection getConnect(String username, String passwd){//驱动类。String driver org.opengauss.Driver;//数据库连接描述符。String sourceURL jdbc:opengauss://10.10.0.13:8000/postgres?enable_ce1;Connection conn null;try{//加载驱动。Class.forName(driver);}catch( Exception e ){e.printStackTrace();return null;}try{//创建连接。conn DriverManager.getConnection(sourceURL, username, passwd);System.out.println(Connection succeed!);}catch(Exception e){e.printStackTrace();return null;}return conn;};说明 【建议】使用JDBC操作密态数据库时一个数据库连接对象对应一个线程否则不同线程变更可能导致冲突。【建议】使用JDBC操作密态数据库时不同connection对密态配置数据有变更由客户端调用isvalid方法保证connection能够持有变更后的密态配置数据此时需要保证参数refreshClientEncryption为1(默认值为1)在单客户端操作密态数据场景下refreshClientEncryption参数可以设置为0。 121.2 调用isValid方法刷新缓存示例
// 创建客户端主密钥
Connection conn1 DriverManager.getConnection(url,user,password);// conn1通过调用isValid刷新缓存
try {if (!conn1.getConnection().isValid(60)) {conn1.getFileWriter().writeLine(isValid Failed for connection 1);}
} catch (SQLException e) {conn1.getFileWriter().writeLine(isValid Failed with error);e.printStackTrace();
}121.3 执行密态等值查询相关的创建密钥语句
// 创建客户端主密钥
Connection conn DriverManager.getConnection(url,user,password);
Statement stmt conn.createStatement();
int rc stmt.executeUpdate(CREATE CLIENT MASTER KEY ImgCMK1 WITH ( KEY_STORE localkms, KEY_PATH \key_path_value\ , ALGORITHM RSA_2048));说明 创建密钥之前需要使用gs_ktool工具提前生成密钥才能创建CMK成功。 // 创建列加密密钥
int rc2 stmt.executeUpdate(CREATE COLUMN ENCRYPTION KEY ImgCEK1 WITH VALUES (CLIENT_MASTER_KEY ImgCMK1, ALGORITHM AEAD_AES_256_CBC_HMAC_SHA256););121.4 执行密态等值查询相关的创建加密表的语句
int rc3 stmt.executeUpdate(CREATE TABLE creditcard_info (id_number int, name varchar(50) encrypted with (column_encryption_key ImgCEK1, encryption_type DETERMINISTIC),credit_card varchar(19) encrypted with (column_encryption_key ImgCEK1, encryption_type DETERMINISTIC)););
// 插入数据
int rc4 stmt.executeUpdate(INSERT INTO creditcard_info VALUES (1,joe,6217986500001288393););
// 查询加密表
ResultSet rs null;
rs stmt.executeQuery(select * from creditcard_info where name joe;);
// 关闭语句对象
stmt.close();121.5 执行加密表的预编译SQL语句
// 调用Connection的prepareStatement方法创建预编译语句对象。
PreparedStatement pstmt con.prepareStatement(INSERT INTO creditcard_info VALUES (?, ?, ?););
// 调用PreparedStatement的setShort设置参数。
pstmt.setInt(1, 2);
pstmt.setString(2, joy);
pstmt.setString(3, 6219985678349800033);
// 调用PreparedStatement的executeUpdate方法执行预编译SQL语句。
int rowcount pstmt.executeUpdate();
// 调用PreparedStatement的close方法关闭预编译语句对象。
pstmt.close();121.6 执行加密表的批处理操作
// 调用Connection的prepareStatement方法创建预编译语句对象。
Connection conn DriverManager.getConnection(url,user,password);
PreparedStatement pstmt conn.prepareStatement(INSERT INTO batch_table (id, name, address) VALUES (?,?,?));
// 针对每条数据都要调用setShort设置参数以及调用addBatch确认该条设置完毕。
int loopCount 20;for (int i 1; i loopCount 1; i) {pstmt.setInt(1, i);pstmt.setString(2, Name i);pstmt.setString(3, Address i);// Add row to the batch.pstmt.addBatch();
}
// 调用PreparedStatement的executeBatch方法执行批处理。
int[] rowcount pstmt.executeBatch();
// 调用PreparedStatement的close方法关闭预编译语句对象。
pstmt.close();点赞你的认可是我创作的动力 ⭐️ 收藏你的青睐是我努力的方向 ✏️ 评论你的意见是我进步的财富