2

I am getting the following error while trying to add a record using my Java JDBC code into SolidDB.

Following the the method that throws the exception:

    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) {
            }
        }
    }
}

I have tried by directly hard-cording the values still it fails. And the insert will be successful if I remove the "ID" & "ENABLED" columns from the insert statement.

Thanks in advance.

Leejoy
  • 1,356
  • 5
  • 23
  • 36
  • What's the exception/stacktrace you get when you run this code? – Alfabravo Aug 21 '15 at 13:57
  • @Alfabravo I have only this info: Nested exception is java.sql.SQLException: [Solid JDBC 7.0.0.2 Build 2012-04-20] SOLID Table Error 13037: Illegal DOUBLE PREC constant. I don't have access to the log. – Leejoy Aug 21 '15 at 13:59
  • According to some user manual out there, that error means you're introducing soble double precision data as a constant and it's not a valid double. Are you sure about the data type of the fields? – Alfabravo Aug 21 '15 at 14:03

2 Answers2

1

PreparedStatement indexes are 1-based (see also setXX javadoc). Because you use 0, and the driver doesn't warn you, everything is "shifted" causing the issue.

Fix:

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");
Community
  • 1
  • 1
1

Finally I got the solution and it is because of a stupid syntax error. When I get rid of the semicolon at the end, it worked!

Leejoy
  • 1,356
  • 5
  • 23
  • 36