gpt4 book ai didi

java - 如何通过 JDBC 的 PreparedStatement 将 UUID 数组插入到 HyperSQL 数据库中

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:50:59 24 4
gpt4 key购买 nike

给定 HyperSQL 中的表格对于 UUID 数组的一列,构建 INSERT PreparedStatement 以填充此类字段的正确方法是什么?

中南合作:

public class Example{
public static void main(String[] args) throws Exception{
// Create UUID array
UUID[] exmapleArray = new UUID[5];

for(int i = 0; i < exmapleArray.length; i++){
exmapleArray[i] = UUID.randomUUID();
}

// Load database class
Class.forName("org.hsqldb.jdbc.JDBCDriver");

// Connect to database
try(Connection databaseConnection = DriverManager.getConnection("jdbc:hsqldb:mem:foo")){
try(Statement createStatement = databaseConnection.createStatement()){
createStatement.executeUpdate("CREATE TABLE bar(test_field UUID ARRAY[10])");
}

try(PreparedStatement preparedStatement = databaseConnection.prepareStatement("INSERT INTO bar VALUES (?)")){
// None of the below examples work

// preparedStatement.setArray(1, databaseConnection.createArrayOf("UUID", exmapleArray));
// preparedStatement.setArray(1, databaseConnection.createArrayOf("VARCHAR", exmapleArray));
// preparedStatement.setArray(1, databaseConnection.createArrayOf("UUID", Arrays.stream(exmapleArray).map(UUID::toString).toArray()));
// preparedStatement.setArray(1, databaseConnection.createArrayOf("VARCHAR", Arrays.stream(exmapleArray).map(UUID::toString).toArray()));

// preparedStatement.setArray(1, new JDBCArrayBasic(exmapleArray, Type.BINARY_UUID));
// preparedStatement.setArray(1, new JDBCArrayBasic(Arrays.stream(exmapleArray).map(UUID::toString).toArray(), Type.BINARY_UUID));

preparedStatement.executeUpdate();
}
}
}
}

虽然可以使用 PreparedStatement.setString() 填充一个普通的 UUID 填充一个数组,但是似乎不起作用,根据选择的不同会产生几个不同的异常上面的语句,最常见的是:

Exception in thread "main" java.sql.SQLSyntaxErrorException: incompatible data type in conversion
at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
at org.hsqldb.jdbc.JDBCConnection.createArrayOf(Unknown Source)
at Example.main(Example.java:31)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
Caused by: org.hsqldb.HsqlException: incompatible data type in conversion
at org.hsqldb.error.Error.error(Unknown Source)
at org.hsqldb.error.Error.error(Unknown Source)
at org.hsqldb.types.CharacterType.convertToDefaultType(Unknown Source)
at org.hsqldb.types.CharacterType.convertJavaToSQL(Unknown Source)
... 7 more

后两个被注释掉的尝试使用 HSQLDB 特定的类(特别是 JDBCArrayBasic),这同样会产生 转换中不兼容的数据类型

最佳答案

我建议使用一个循环来填充您的 PreparedStetement,到 insert an array你必须使用例如:

INSERT INTO bar VALUES ARRAY[3.45, 23.64, 14.01]

所以要填充你的数组,你必须使用这样的循环:

String query = "INSERT INTO bar VALUES ARRAY[";
String del = "";
for (int i = 0; i < 10; i++) {
query += del+"?" ;
del = ",";
}
query += "]";
System.out.println(query);

到目前为止,您的查询如下所示:

INSERT INTO bar VALUES ARRAY[?,?,?,?,?,?,?,?,?,?]

你可以这样完成:

PreparedStatement preparedStatement = connection.prepareStatement(query);
for (int i = 1; i <= 10; i++) {
preparedStatement.setString(i, "valu_"+i);
}
System.out.println("Query = " + preparedStatement.toString());

所以最后你的 preparedStetement 看起来像这样:

INSERT INTO bar VALUES ARRAY['valu_1','valu_2','valu_3','valu_4','valu_5','valu_6','valu_7','valu_8','valu_9','valu_10']

希望这能给你一个想法。

关于java - 如何通过 JDBC 的 PreparedStatement 将 UUID 数组插入到 HyperSQL 数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42757143/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com