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
How create a SQL array from a Java List?
提问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)
回答by JB Nizet
Use "varchar" instead of "VARCHAR". See http://grepcode.com/file/repo1.maven.org/maven2/postgresql/postgresql/9.0-801.jdbc4/org/postgresql/jdbc2/TypeInfoCache.java#TypeInfoCache.0types
使用“varchar”而不是“VARCHAR”。见http://grepcode.com/file/repo1.maven.org/maven2/postgresql/postgresql/9.0-801.jdbc4/org/postgresql/jdbc2/TypeInfoCache.java#TypeInfoCache.0types