SQL prepareStatement不返回ResultSet

时间:2014-08-04 12:42:39

标签: java prepared-statement

我正面临一些奇怪的行为,有两个准备好的陈述。 我正在做的是检查我想保存到数据库的doument是否存在于当前版本中。如果db没有返回一个条目,我会检查db中是否有旧版本更新它。如果找不到任何内容(不是当前版本而不是旧版本),我将它们保存为新版本。

我的代码如下所示:

try {
    stmt = connection.prepareStatement("SELECT col1, col2, col3, col4" +
                                                "FROM table" +
                                                "WHERE col1= (SELECT col1 FROM table WHERE col2= ? and col3= ?) " +
                                                "AND col2= (SELECT col1 FROM table WHERE col2= ? and col3= ?) " +
                                                "AND col3 = ? " +
                                                "AND col4= ?");
    stmt.setString(1, empm.col1().toString());
    stmt.setString(2, empm.col2().trim());
    stmt.setString(3, empm.col1().toString());
    stmt.setString(4, empm.col2().trim());
    stmt.setShort(5, empm.col3());
    stmt.setShort(6, empm.col4());

    ResultSet stmtResult = stmt.executeQuery();
    // Check if current version is available
    if(stmtResult.next()) {
        Integer existingCol1 = stmtResult.getInt(1);
        String existingCol2 = stmtResult.getString(2).trim();
        Short existingCol3 = stmtResult.getShort(3);
        Short existingCol4 = stmtResult.getShort(4);
        empm.setCol1(null);
        empm.setCol2(null);

        empm.update(connection, "WHERE col1  = " + existingCol1 + " AND col2 = '" + existingCol2 + "' AND col3 = " + existingcol3 + " AND col4 = " + existingCol4);
    } else {
        // Check if older version is available
        PreparedStatement empmHasOlderVersionSql;
        empmHasOlderVersionSql = connection.prepareStatement("SELECT col1, col2, col3, col4" +
                                        "FROM table" +
                                        "WHERE col1= (SELECT col1 FROM table WHERE col2= ? and col3= ?) " +
                                        "AND col2= (SELECT col1 FROM table WHERE col2= ? and col3= ?) " +
                                        "AND col3 = ? " +
                                        "AND col4= ?");
        empmHasOlderVersionSql.setString(1, empm.getPbnr().toString());

        Integer olderVersion = (Integer.parseInt(empm.getCol1().trim())-1);
        empmHasOlderVersionSql.setString(2, olderCol2.toString());

        empmHasOlderVersionSql.setString(3, empm.getCol1().toString());
        empmHasOlderVersionSql.setString(4, olderCol2.toString());
        empmHasOlderVersionSql.setShort(5, empm.getCol3());
        empmHasOlderVersionSql.setShort(6, empm.getCol4());

        ResultSet empmHasOlderVersionResult = empmHasOlderVersionSql.executeQuery();

        // If I found an older version update it with new versions
        if(empmHasOlderVersionResult.next()) {
            System.out.println("Older");
            Integer existingCol1 = empmHasOlderVersionResult.getInt(1);
            String existingcol2 = empmHasOlderVersionResult.getString(2).trim();
            Short existingCol3 = empmHasOlderVersionResult.getShort(3);
            Short existingCol4 = empmHasOlderVersionResult.getShort(4);


            // Update with new versions
            Integer intCol2 = (Integer.parseInt(existingCol2) + 1);
            String newCol2 = intCol2.toString();

            empm.setCol2(newVersion);
            empm.update(connection, "WHERE col1 = " + existingCol1 + " AND col2 = '" + existingCol2 + "' AND col3 = " + existingCol3 + " AND col4 = " + existingCol4);
        } else {

            empm.setCol1(emp.getCol1());
            empm.setCol2(emp.getCol2());

            empm.save(connection);
        }
    }
} catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    errorMessage = errorMessage.concat("Error while saving entry in empm. ");
}

当我有例如。 DB和文件中的Col1 = 100Col2 = 1。一切正常。 但是,如果我在col1 = 100Col2 = 2中,代码会插入一个条目,而不是更新到新的Col2 = 2,而col1null

执行语句的唯一区别是Col2值。 有人知道什么可能是错的吗? 我使用MSSQL2012 DBjtds 1.2.5 Eclipse IDE

修改: 我终于发现这是一个交易问题。 使用connection.autoCommit(true)运行应用程序时,一切正常。 任何人都有关于如何设置提交或保存点等的一些问题?

0 个答案:

没有答案