java - PostgreSQL/JDBC - 如何准备调用带有参数 CITEXT (TEXT) 的函数?

标签 java postgresql jdbc plpgsql postgresql-9.4

我创建了一个包含 CITEXT 列的表,CITEXT(TEXT 的不区分大小写的版本)是我之前使用 加载的扩展code>创建扩展 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;

现在我正在尝试使用 java 调用函数

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;
}

如果传递给 sql 函数的参数是 INTEGERVARCHAR(n) 类型,则调用函数的相同方法也能正常工作。

我假设 callableStatement.setString(2, name); 抛出 SQLException 因为 setString() 将用于 VARCHAR (n) 个字段?

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 没有像 setText(int i, String text) 这样的方法 - 我必须改用什么?

最佳答案

我终于明白了。我所要做的就是使用 ::citext 将参数转换为 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;
}

关于java - PostgreSQL/JDBC - 如何准备调用带有参数 CITEXT (TEXT) 的函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32833674/

相关文章:

java - Spring Framework 中的 Redis 设置

postgresql - 动态 SQL 中的参数串联

java - MySQL JDBC 驱动错误

mysql - 如果 jdbc.queryForObject 不返回行,如何处理

mysql - 无法连接到 GCP 上的云 sql

java - Java EE 中的异步执行

java - 不同名称、相同内容的文件的哈希函数

Java ActionListener(多个语句)

python - Mac OS下安装pygresql时出现clang错误

postgresql - postgres - 将列转置为行