PSQLException:错误:运算符不存在:bigint = text []

时间:2019-11-28 07:55:19

标签: java postgresql jdbc

在postgres DB和MySQL DB中寻找解析效果很好

I / P:字符串ordIds =“ 343,21,343”;

映射fetchDataSource(String orgIds){

    ResultSet resultSet = null;
    PreparedStatement statement = null;
    String str[] = orgIds.split(",");
    List<Integer> orgIdList = new ArrayList<Integer>();

    Map<String, String> dataSourceMap = new LinkedHashMap<String, String>();
    orgIdList = Arrays.asList(str).stream().map(Integer::valueOf).collect(Collectors.toList());
    String query = "select ds.ds_path,string_agg(org.organization_id::text, ',') as org_id from c_organization org join org_ds_detail ds on org.org_ds_detail_id = ds.org_ds_detail_id where org.organization_id in (?) GROUP BY ds.ds_path";
    //String queryIN = orgIdList.stream().map(orgId -> String.valueOf(orgId)).collect(Collectors.joining(",", "(", ")"));

    try {
        Connection connection = DBConnection.getInstance("CLINICS_GLOBAL");
        statement = connection.prepareStatement(query);
        **Array orgIdsInArray = connection.createArrayOf("text",orgIdList.toArray());
        statement.setArray(1, orgIdsInArray);**
        logger.info("Executing Query:" + query);
        resultSet = statement.executeQuery();
        while (resultSet.next()) {
            dataSourceMap.put(resultSet.getString(1), resultSet.getString(2));
        }

    } catch (SQLException e) {
        logger.info("Exception:" + e);
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            statement.close();
            resultSet.close();
            DBConnection.closeConnection();
        } catch (SQLException e) {
            logger.info("Exception while closing connection:" + e.getMessage());
            e.printStackTrace();
        }
    }
    return dataSourceMap;
}

异常(日志):

13:16:16,217 INFO  [DataSourceConnection] (default task-2) Exception:org.postgresql.util.PSQLException: ERROR: operator does not exist: bigint = text[]
  Hint: No operator matches the given name and argument type(s). You might need to add explicit type casts.
  Position: 190
13:16:16,219 ERROR [stderr] (default task-2) org.postgresql.util.PSQLException: ERROR: operator does not exist: bigint = text[]
13:16:16,219 ERROR [stderr] (default task-2)   Hint: No operator matches the given name and argument type(s). You might need to add explicit type casts.
13:16:16,220 ERROR [stderr] (default task-2)   Position: 190
13:16:16,221 ERROR [stderr] (default task-2)    at 

1 个答案:

答案 0 :(得分:0)

好吧,正如错误消息所述,您正在传递一个text[]数组,并试图将其与一个bigint值进行比较。您希望Long.valueOf(1) == new String[] {"1", "2"}在Java中工作吗?

您需要创建一个bigint数组:

Array orgIdsInArray = connection.createArrayOf("bigint",orgIdList.toArray());

您需要更改将列与数组进行比较的运算符:

where org.organization_id = any (?)

另一个不涉及创建java.sql.Array的选项是将用逗号分隔的列表转换为SQL而不是Java的数组。为此,将您的SQL语句更改为:

where org.organization_id = any (string_to_array(?, ',')::bigint[])

然后只需将原始字符串传递给PreparedStatement:

statement.setString(1, orgIds);

然后您可以摆脱将String转换为数组,然后转换为Java中的List的麻烦。