HyperSQL의 테이블과 UUID 배열의 열이 주어진 경우 INSERT
PreparedStatement
을 작성하여 이러한 필드를 채우는 올바른 방법은 무엇입니까?JDBC의 PreparedStatement를 통해 UUID 배열을 HyperSQL 데이터베이스에 삽입하는 방법
SSCCE :
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();
}
}
}
}
일반 UUID
을 채우는하는 것은 그러나 위의 진술의 선택, 가장 일반적인 존재에 따라 여러 가지 예외를 산출 작동하지 않는 배열을 채우는 PreparedStatement.setString()
함께 할 수있는 동안 : 주석 처리 시도
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
후자의 두 유사 incompatible data type in conversion
를 산출 (구체적 JDBCArrayBasic
) HSQLDB 특정한 클래스를 사용한다.