3

I created a table with a CITEXT column, CITEXT (case-insensitive version of TEXT) being an extension I previously loaded using CREATE EXTENSION citext;

CREATE TABLE artists (
    artist_id   SERIAL   PRIMARY KEY,
    artist      CITEXT   UNIQUE NOT NULL
);

CREATE OR REPLACE FUNCTION add_artist(_artist CITEXT) RETURNS INTEGER AS $$
    DECLARE
        _artist_id INT;
    BEGIN
        SELECT artist_id INTO _artist_id FROM artists WHERE artist = _artist;

        IF (_artist_id IS NULL) THEN
            INSERT INTO artists (artist) VALUES (
                _artist
            ) RETURNING artist_id INTO _artist_id;
        END IF;

        RETURN _artist_id;
    END;
$$ LANGUAGE plpgsql;

Now I am trying to call the function in java using

public int addArtist(String name) throws SQLException {
    int artist_id;

    try (CallableStatement callableStatement = 
            connection.prepareCall("{ ? = CALL add_artist(?) }")) {
        callableStatement.registerOutParameter(1, Types.INTEGER);
        callableStatement.setString(2, name);
        callableStatement.execute();
        artist_id = callableStatement.getInt(1);
    }

    return (artist_id != 0) ? artist_id : -1;
}

The same method in calling functions works fine if the argument passed to the sql function is type of INTEGER or VARCHAR(n).

I assume callableStatement.setString(2, name); is throwing the SQLException because setString() is to be be used for VARCHAR(n) fields?

org.postgresql.util.PSQLException: ERROR: function add_artist(character varying) does not exist
  Hint: No function matches the given name and argument types. You might need to add explicit type casts.
  Position: 15
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2182)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1911)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:173)
at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:615)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:465)
at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:458)
at postgresql.PostgreSQL.addArtist(PostgreSQL.java:61)
at postgresql.PostgreSQLConnector.main(PostgreSQLConnector.java:26)

CallableStatement has no methods like setText(int i, String text) - What must I use instead?

1 Answer 1

6

I eventually figured it out. All I had to do was casting the parameter to CITEXT using ::citext.

public int addArtist(String name) throws SQLException {
    int artist_id;

    try (CallableStatement callableStatement = 
            connection.prepareCall("{ ? = CALL add_artist(?::citext) }")) {
        callableStatement.registerOutParameter(1, Types.INTEGER);
        callableStatement.setString(2, name);
        callableStatement.execute();
        artist_id = callableStatement.getInt(1);
    }

    return (artist_id != 0) ? artist_id : -1;
}
4
  • 1
    The other option is to set stringtype=unspecified as a connection parameter. Sep 29, 2015 at 0:30
  • Interesting, is that more common than casting? And are there differences between setting stringtype=unspecified and casting the argument within the prepareCall() string?
    – phew
    Sep 29, 2015 at 0:43
  • 2
    Casting is preferable if you're able to. If you're using some ORM that doesn't give you that level of control, passing stringtype=unspecified asks PgJDBC to treat strings like untyped literals and let the database server infer their data type. Sep 29, 2015 at 0:43
  • 1
    Thank you for the information, then I'll go with casting. Nevertheless, tempting to use the stringtype parameter out of lazyness.
    – phew
    Sep 29, 2015 at 0:47

Your Answer

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

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