How do I pass an array of timestamps to the Postgres function using Java and JDBC?

Here's the signature of the function:

CREATE OR REPLACE FUNCTION getlistcount(_id integer, _subs text, _download_array timestamp without time zone[], _filter integer, _fault text) RETURNS refcursor AS...

Here is the Java code that creates the array:

GregorianCalendar[] dataDownloads = 
        downloadTimes.toArray(new GregorianCalendar[downloadTimes.size()]);

Array sqlArray = conn.createArrayOf("timestamp", dataDownloads);
cstmt.setArray(4, sqlArray);

The downloadTimes are List passed into the Java function.

One hint: we need to concatenate the time with the date, according to the Postgres documentation.

link|improve this question
1  
I see that you're using TIMESTAMP WITHOUT TIME ZONE. In almost every use case, TIMESTAMP WITH TIME ZONE is better; it represents an actual moment in time, while the other does not. – kgrittn yesterday
@KGrittn, that makes sense. We're getting the timezone from the download-time (see below), save the timezone with the timestamp. Thank you! – MAbraham1 yesterday
feedback

1 Answer

I found an answer on StackOverflow that is similar, but uses the PreparedStatement rather than the JDBC notation.

//Get timezone from first entry, assuming all same timezone
if(!downloadTimes.isEmpty()) {
    cal.setTimeZone(downloadTimes.get(0).getTimeZone());
}

//Create an array of timestamps
java.sql.Timestamp [] array = new java.sql.Timestamp [downloadTimes.size()];

for(int i=0; i < array.length; i++) {
    array[i] = new java.sql.Timestamp(downloadTimes.get(i).getTimeInMillis());
} 

//pass the array to JDBC call
//conn is the connection, and cstmt is the CallableStatement
Array sqlArray = conn.createArrayOf("timestamp", array);
cstmt.setArray(4, sqlArray);
link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.