【标题描述】: 在BLOB MODE打开的情况下无法通过setBinaryStream()方法插入blob数据
【测试类型:接口功能】
【测试版本:MASTER】
【操作步骤】(请填写详细的操作步骤):
1.在opengauss数据库上创建表:
/**
* @author saxisuer
* huawei=> create table test_lob (id varchar(64), col_blob blob, col_bytea bytea);
* CREATE TABLE
* huawei=> \d test_lob
* Table "test_data.test_lob"
* Column | Type | Modifiers
* -----------+-----------------------+-----------
* id | character varying(64) |
* col_blob | blob |
* col_bytea | bytea |
*/
/**
* blobMode=on
* 通过setBinaryStream的方式 插入数据到blob,bytea 字段中
*
* @throws ClassNotFoundException
* @throws SQLException org.opengauss.util.PSQLException: [127.0.0.1:53558/.0.0.0/0.0.0.0:55448] ERROR: column "col_blob" is of type blob but expression is of type bytea
* 建议:You will need to rewrite or cast the expression.
* 位置:67
* 在位置:referenced column: col_blob
**/
@Test
public void testBlobModeOnfInsert() throws ClassNotFoundException, SQLException, IOException {
Class.forName(org.opengauss.Driver.class.getName());
blobMode = true;
String url = buildJdbcUrl();
System.out.println(url);
Connection connection = DriverManager.getConnection(url, "dbuser", "dbpwd");
InputStream inputStream = new ByteArrayInputStream(content.getBytes());
InputStream inputStream1 = new ByteArrayInputStream(content.getBytes());
PreparedStatement preparedStatement = connection.prepareStatement(insertSQL);
preparedStatement.setString(1, UUID.randomUUID().toString());
preparedStatement.setBinaryStream(2, inputStream);
preparedStatement.setBinaryStream(3, inputStream1);
preparedStatement.execute();
preparedStatement.close();
connection.close();
}
【预期输出】:
正常插入 col_blob 字段 但是 col_typea 字段报错
【实际输出】:
column "col_blob" is of type blob but expression is of type bytea
【原因分析】:
【日志信息】(请附上日志文件、截图、coredump信息):
【测试代码】:
Hey @萨西摩尔, Welcome to openGauss Community.
All of the projects in openGauss Community are maintained by @opengauss-bot.
That means the developers can comment below every pull request or issue to trigger Bot Commands.
Please follow instructions at Here to find the details.
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。
Hi @萨西摩尔,
if you want to get quick review about your issue, please contact the owner in first: @周斌 ,
and then any of the maintainers: @Pike
and then any of the committers: @Kamus , @vimiix , @汪伟 , @travelliu , @aaronwell , @Loong
if you have any question, please contact the SIG: Connectors.
验收日期:2023-10-21(5.0.1 B006)
验收版本:compiled at 2023-10-19-10:19:59 build 6061008
验收结果:通过
验收脚本
package PreparedStatement;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.UUID;
import org.opengauss.Driver;
/**
* @作者 : pwx5328113 @时间 : 2023/9/27
* @测试点 : blob字段类型setBinaryStream\setBlob\getBlob方法覆盖
**/
public class JDBC_PreparedStatement_Case0002 {
public static void main(String[] args) throws Exception {
if (args.length < 4) {
System.out.print("need para: host:ip,db_b,user,password");
return;
}
System.out.println(Driver.getGSVersion());
String t_name = "t_jdbc_preparedstatament_0027";
String url1 = "jdbc:opengauss://" + args[0] + "/" + args[1] + "?LoggerLevel=off&blobMode=on";
Connection connection1 = DriverManager.getConnection(url1, args[2], args[3]);
Statement statement = connection1.createStatement();
String create_sql = "drop table if exists " + t_name + "; " + "create table " + t_name
+ " (id varchar(64), col_blob blob);";
statement.executeUpdate(create_sql);
String insert_sql = "insert into " + t_name + " (id, col_blob) values (?, ?)";
String select_sql = "select id,col_blob from " + t_name;
String content = "通过setBinaryStream方法插入blob数据";
InputStream inputStream1 = new ByteArrayInputStream(content.getBytes());
PreparedStatement preparedStatement = connection1.prepareStatement(insert_sql);
preparedStatement.setString(1, UUID.randomUUID().toString());
preparedStatement.setBinaryStream(2, inputStream1);
preparedStatement.execute();
System.out.println("testcase1:通过setBinaryStream方法插入blob数据成功");
content = "通过setBlob方法插入blob数据";
inputStream1 = new ByteArrayInputStream(content.getBytes());
preparedStatement.setString(1, UUID.randomUUID().toString());
preparedStatement.setBlob(2, inputStream1);
preparedStatement.execute();
System.out.println("testcase2:通过setBlob方法插入blob数据成功");
try {
preparedStatement.setString(1, UUID.randomUUID().toString());
preparedStatement.setBytes(2, content.getBytes());
preparedStatement.execute();
} catch (Exception e) {
System.out.println("testcase3:通过setBytes方法插入blob数据报错 \n" + e.getMessage());
}
try (ResultSet resultSet = statement.executeQuery(select_sql)) {
while (resultSet.next()) {
String col1 = resultSet.getObject(1).toString();
Object col2 = resultSet.getObject(2);
Blob col3 = resultSet.getBlob(2);
System.out.println("col1 = " + col1);
System.out.println("testcase4:getObject获取blob字段 = " + new String((byte[]) col2)); // getObject获取结果以字符串方式显示
System.out.println("testcase5:getBlob获取blob字段 = " + new String(col3.getBytes(1, (int) col3.length()))); // getBlob获取结果以字符串方式显示
}
}
statement.executeUpdate("drop table " + t_name);
preparedStatement.close();
statement.close();
statement.close();
}
}
验收日期:2023-11-30
验收版本:compiled at 2023-11-28-19:27:06 build 2eed94f
验收结论:通过
登录 后才可以发表评论