Markie Doe Markie Doe - 9 months ago 54
SQL Question

Postgresql error when trying to insert Long array

ArrayList temp2 = new ArrayList();

Im filling this arraylist with longs.

When i am trying to insert this into the postgresql, i use the following:

UPDATE pb1plnitm SET parents_pb1plnitm_unid = ARRAY[?] WHERE pb1plnitm_unid = ?", temp2, xmlForm.getFieldValueLong("uniqueid"));

Using JDBC template. But when i try to run this code it gives a error on the "ARRAY[?]". But if temp2 is a single number like: 1253214, it works. Does anyone have an idea?

My error is:

PreparedStatementCallback; bad SQL grammar [UPDATE pb1plnitm SET parents_pb1plnitm_unid = ARRAY[?] WHERE pb1plnitm_unid = ?]; nested exception is org.postgresql.util.PSQLException: Can't infer the SQL type to use for an instance of [Ljava.lang.Long;. Use setObject() with an explicit Types value to specify the type to use.

Answer Source

as mentioned in error description, make use of setObject() method

For Example:

Array sqlArray = conn.createArrayOf("text", anyArray);
    pstmt.setArray(1, sqlArray);