本文介绍了java.sql.PreparedStatement.setArray():不支持的交叉转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用jdbc运行以下查询时,我收到以下异常。
这是一个简化的例子,prepareStatementSetter的其他部分与setString和setInt等成功运行。

  public List< EmployeeMonitoringResultTo> searchEmployeesForEmployerInRange(最终GetEmployeesForEmployerInRangeCriteria条件){
final StringBuilder sql =
new StringBuilder(select * from CODES c)
.append(其中c.TYPE in(?)); // TYPE has DATA_TYPE 12,TYPE_NAME VARCHAR

return jdbcTemplate.query(
sql.toString(),
new PreparedStatementSetter(){
@Override
public void setValues(PreparedStatement ps)throws SQLException {
ps.setArray(1,searchTypes());
}
},
new SpecificRowMapper&SpecType>()
);
}

private Array searchTypes(){
final Collection< String> collection = fetchStrings();

数组resultArray = null ;;
try {
resultArray = jdbcTemplate.getDataSource()。getConnection()。createArrayOf(VARCHAR,collection.toArray());
} catch(SQLException e){
log.error(创建的java.sql.Array的问题,e);
throw e;
}

return resultArray;
}




解决方案

toArray方法返回一个对象数组。你可以尝试使用一串数组:

  collection.toArray(new String [0]); 

您可能无法使用带IN操作符的SQL数组,因此您必须生成SQL而不是使用准备好的语句。 (或至少生成正确数量的问号,然后将参数设置为循环,如评论中建议的那样。)



您还缺少其中关键字。


I'm getting the exception shown below, when I run the following query with jdbc.This is a simplified example, the other parts of the preparedStatementSetter with setString and setInt, etc. ran successfully.

public List<EmployeeMonitoringResultTo> searchEmployeesForEmployerInRange(final GetEmployeesForEmployerInRangeCriteria criteria) {
    final StringBuilder sql =
    new StringBuilder(  "select * from CODES c ")
                .append("    where c.TYPE in ( ? )  "); // TYPE has DATA_TYPE 12, TYPE_NAME VARCHAR

    return jdbcTemplate.query(
        sql.toString(),
        new PreparedStatementSetter() {
            @Override
            public void setValues(PreparedStatement ps) throws SQLException {
                ps.setArray(1, searchTypes());
            }
        },
        new SpecificRowMapper<SpecType>()
        );
}

private Array searchTypes() {
    final Collection<String> collection = fetchStrings();

    Array resultArray = null;;
    try {
        resultArray = jdbcTemplate.getDataSource().getConnection().createArrayOf("VARCHAR", collection.toArray());
    } catch (SQLException e) {
        log.error("Problem with created java.sql.Array", e);
        throw e;
    }

    return resultArray;
}
解决方案

The toArray method returns an array of objects. You could try using an array of strings:

collection.toArray(new String[0]);

You probably can't use an SQL array with an IN operator anyway, so you'll have to generate SQL instead of using a prepared statement. (Or at least generate the correct number of question marks and then set the parameters in a loop, like suggested in the comments.)

You are also missing the where keyword.

这篇关于java.sql.PreparedStatement.setArray():不支持的交叉转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 14:29