HSQLDB隐藏异常消息:"功能不受支持"

Jam*_*ams 9 java sql exception jdbc hsqldb

我有JDBC代码,通过执行PreparedStatement插入到数据库表中.当我在内存中的HSQLDB数据库上运行代码时(作为JUnit测试的一部分),我得到一个SQLFeatureNotSupportedException,唯一的信息是消息"功能不受支持"和供应商代码-1500.我正在做的是基本插入表 - 我无法想象这在最新的HSQLDB中是不受支持的.

我的代码:

public Observations saveOrUpdate(final Observations observations)
{
    try
    {
        if (connection == null)
        {
            connection = getJdbcTemplate().getDataSource().getConnection();
        }

        // create the prepared statement
        String sql = "INSERT INTO " + Observations.TABLE_NAME +
                     " (OBS_YEAR, WINTER, SPRING, SUMMER, FALL, ANNUAL, DATA_TYPE, CREATED_DATE, UPDATED_DATE, " +
                     Observations.ID_COLUMN_NAME +
                     ") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setInt(1, observations.getYear());
        preparedStatement.setBigDecimal(2, observations.getJan());
        preparedStatement.setBigDecimal(3, observations.getFeb());
        preparedStatement.setBigDecimal(4, observations.getMar());
        preparedStatement.setBigDecimal(5, observations.getApr());
        preparedStatement.setBigDecimal(6, observations.getMay());
        preparedStatement.setString(7, observations.getDataType().toString());
        preparedStatement.setTimestamp(8, new Timestamp(observations.getCreatedDate().getTime()));
        preparedStatement.setTimestamp(9, new Timestamp(observations.getUpdatedDate().getTime()));
        preparedStatement.setLong(10, observations.getId());
        preparedStatement.executeUpdate(sql);

        return observations;
    }
    catch (SQLException ex)
    {
        throw new RuntimeException(ex);
    }
}
Run Code Online (Sandbox Code Playgroud)

任何人都可以建议可能是什么问题或我应该进一步研究的其他事情?在此先感谢您的帮助.

- 詹姆士

Tho*_*ler 11

你需要调用preparedStatement.executeUpdate()(没有参数sql).

您调用了该方法PreparedStatement.executeUpdate(String sql),根据JDBC规范,该方法是非法的.再次传递SQL语句没有任何意义,因为您在创建PreparedStatement对象时已经传递了它.即使您认为传递相同的字符串,调用此方法也是不合法的.调用方法不合法有点奇怪:-)但这就是它的方式.在这种情况下,所有符合标准的JDBC驱动程序都需要抛出异常.

但我同意错误信息是神秘的.