尝试使用Java JDBC代码将记录添加到SolidDB时出现以下错误。

遵循引发异常的方法:

    public void addData(User data) {
    Connection conn = null;
    try {
        conn = dataSource.getConnection();
        if (conn == null || conn.isClosed()) {
            throw new RuntimeException("Could not open connection");
        }
        String query="INSERT INTO USER "
                + "(ID, NAME, PASSWORD, ENABLED, REQUIRE_RESET)"
                + "VALUES (?, ?, ?, ?, ?);";

        PreparedStatement ps = conn.prepareStatement(query);
        // "ID" column is INTEGER, and partition field in UserData is int.
        ps.setString(1, data.getId());
        ps.setString(2, data.getName());
        ps.setString(3, data.getPassword());
        // "ENABLED" column is INTEGER, and enabled field in UserData is boolean.
        ps.setInt(4, data.isEnabled()? 1:0);
        ps.setString(5, data.isRequireReset()?"Y":"N");
        if (ps.executeUpdate() < 1) {
            throw new RuntimeException("Could not save User, executeUpdate() returned a value < 1");
        }

    } Catch (SQLException e) {
        throw new DataLayerException("Could not save the User Data.", e);
    } finally {
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
            }
        }
    }
}


我尝试通过直接对值进行硬编码仍然失败。
如果我从插入语句中删除“ ID”和“ ENABLED”列,则插入将成功。

提前致谢。

最佳答案

PreparedStatement索引为1-based(另请参见setXX javadoc)。因为您使用0,并且驱动程序没有警告您,所以一切都“移动”了,导致了问题。

固定:

ps.setString(1, data.getId());
ps.setString(2, data.getName());
ps.setString(3, data.getPassword());
ps.setInt(4, data.isEnabled()? 1:0);
ps.setString(5, data.isRequireReset()?"Y":"N");

关于java - SOLID Table Error 13037:非法的PREC常量,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32141582/

10-13 04:22