Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a postgres DB containing a table with a boolean array for the days of the week (t/f whether the employee has to work on this day or not).

The table is as follows:

CREATE TABLE employee(
        ...,
    weekdays BOOLEAN[7],
        ...
);

In my Java servlet I would like to read and write the array using a boolean[] (primitive type). I have found out that it should work with the java.sql.Array Object.

Inserting seems to work with the following:

Connection dbcon = DataSource.checkOut();

preparedStatement pstatement = dbcon.prepareStatement("INSERT INTO employee (weekdays) VALUES (?)");
pstatement.setArray(1, dbcon.createArrayOf("BOOLEAN", castToObjectType( objectToInsert.getWorkingdays() ).toArray() ) );

...

private ArrayList<Boolean> castToObjectType(boolean[] valueArray){
    ArrayList<Boolean> objArray = new ArrayList<Boolean>();

    for (int i = 0; i < valueArray.length; i++) {
        objArray.add( new Boolean(valueArray[i]) );
    }

    return objArray;
}

I don't know if this is a good way to do it and I'm also somehow stuck the way round doing the conversion from java.sql.Array to boolean[]. Ideas, Best practices?

share
add comment

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.