postgresql 如何从 Java 列表创建 SQL 数组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7931654/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-20 23:23:11  来源:igfitidea点击:

How create a SQL array from a Java List?

javasqlarrayspostgresql

提问by Renato Dinhani

I'm trying to insert an array in a table, but I need to convert the list to the SQL Array type. I'm using the Connection#createArrayOf()method, but I'm getting an exception.

我正在尝试在表中插入一个数组,但我需要将列表转换为 SQL 数组类型。我正在使用该Connection#createArrayOf()方法,但出现异常。

I need to pass a type name, but I don't know what is this and I always get an exception. The array is from VARCHAR.

我需要传递一个类型名称,但我不知道这是什么,我总是得到一个例外。该数组来自 VARCHAR。

How I solve this to insert the array?

我如何解决这个问题以插入数组?

The code

代码

Object[] array = new Object[token.getCategories().size()];
array = token.getCategories().toArray();
pstmTokenInsert.setArray(1, conn.createArrayOf("VARCHAR", array));

The stacktrace

堆栈跟踪

org.postgresql.util.PSQLException: Could not find array type for data type VARCHAR
at org.postgresql.jdbc4.AbstractJdbc4Connection.createArrayOf(AbstractJdbc4Connection.java:73)
at org.postgresql.jdbc4.Jdbc4Connection.createArrayOf(Jdbc4Connection.java:21)
at org.apache.commons.dbcp.DelegatingConnection.createArrayOf(DelegatingConnection.java:560)
at br.ifsp.da.data.TokenDAO.insertTokens(TokenDAO.java:37)
at br.ifsp.da.data.ProcessedPageInserter.loopInsertion(ProcessedPageInserter.java:44)
at br.ifsp.da.data.ProcessedPageInserter.call(ProcessedPageInserter.java:27)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
at java.util.concurrent.FutureTask.run(FutureTask.java:166)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722)