6

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.

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.

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)
5
  • Are you using Oracle? The SQL array type highly depends on the underlying database. Commented Oct 28, 2011 at 15:53
  • 1
    Can you post the stacktrace of the Exception ? :) Commented Oct 28, 2011 at 15:54
  • I guess this would be helpful for you: download.oracle.com/javase/tutorial/jdbc/basics/array.html Commented Oct 28, 2011 at 15:59
  • @kocko I posted the stacktrace. Commented Oct 28, 2011 at 15:59
  • @DaveNewton 9.0-801 of the PostgreSQL driver for Java Commented Oct 28, 2011 at 19:28

2 Answers 2

18

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

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you, master. Helped me a lot. Was a simple mistake (that take me some hours), but I didn't found this information anywhere. Thanks.
when I change to 'varchar' I got this error. org.postgresql.util.PSQLException: ERROR: operator does not exist: character varying = character varying[] Hint: No operator matches the given name and argument type(s). You might need to add explicit type casts
@Damith You can use =ANY operator instead of IN
0

I prefer to used the enum. Use PostgresSqlType.STRING.getValue() instead.

Below are the possible values of different type. These are case sensitive so check the case.

public enum PostgresSqlType {
    SHORT("int2"),
    INT("int4"),
    LONG("int8"),
    FLOAT("float4"),
    DOUBLE("float8"),
    BOOLEAN("bool"),
    STRING("varchar"), // or "text"
    BYTE("bytea");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.