1

I have a ResultSet which is returning 50 rows. I need to have a temporary table to which I insert these 50 rows so as I can perform queries on it.

There is no alternative for this so please don't suggest having a subquery or something else. A temporary table is needed.

So I am inserting the rows with the following method and apparently although I know the ResultSet consists of 50 rows, it is looping only for 13 times in the while loop and therefore when I go to extract some fields from this table, I do not have the required result.

public void insertValues(Connection con, ResultSet rs) {

    StringBuffer insert_into_temp = new StringBuffer();

    try {
        ResultSetMetaData rsmd = rs.getMetaData();
        int colCount = rsmd.getColumnCount();


        insert_into_temp.append("INSERT INTO SESSION.RETURNED_TICKETS (");

        for (int i = 1; i <= colCount; i++) {
            insert_into_temp.append(rsmd.getColumnLabel(i)); 
            insert_into_temp.append(",");
        }

        insert_into_temp.deleteCharAt(insert_into_temp.length()-1);
        insert_into_temp.append(")");

        insert_into_temp.append("\nVALUES(");

        // number of place-holders for values
        for (int i = 0; i < colCount; i++) {
             insert_into_temp.append("?,");  
        }

        insert_into_temp.deleteCharAt(insert_into_temp.length()-1);
        insert_into_temp.append(")");

        while(rs.next()){
            PreparedStatement pstmt = con.prepareStatement(insert_into_temp.toString());

            pstmt.setInt(1, rs.getInt(Ticket.FLD_ID));
            pstmt.setString(2, rs.getString(Ticket.FLD_DESCRIPTION));
            pstmt.setInt(3, rs.getInt(Ticket.FLD_TICKETTYPE));
            pstmt.setString(4, rs.getString("STATE"));
            pstmt.setString(5, rs.getString("PRIORITY"));
            pstmt.setString(6, rs.getString("OWNER"));
            pstmt.setString(7, rs.getString("SUBMITTER"));
            pstmt.setString(8, rs.getString("TYPE"));
            pstmt.setString(9, rs.getString(Ticket.FLD_TITLE));
            pstmt.setString(10, rs.getString("PROJECT"));
            pstmt.setInt(11, rs.getInt("PROJID"));
            pstmt.setDouble(12, rs.getDouble("RELEASE"));
            pstmt.setTimestamp(13, rs.getTimestamp(Ticket.FLD_SUBMITDATE));
            pstmt.setInt(14, rs.getInt(Ticket.FLD_CUSTOMER));  
            pstmt.setInt(15, rs.getInt("ROW_NEXT")); 

            int success = pstmt.executeUpdate();

            if (success != 1) // if not successful
                throw new SQLException("Failed to insert values into temporary table for linked/unlinked tickets");

        }

    } catch (SQLException e){
        LogFile.logError("[Report.execute()] "+e.getMessage());
        LogFile.logError(insert_into_temp.toString());
    }
}

What can be the problem? I can't figure out why this is happening. Thanks

4

2 回答 2

0

如果我正确理解了问题,而不是像这样每次使用都执行您的查询,

con.setAutoCommit(false); while (rs.next()) { // your setting values on prepared statement code pstmt.addBatch(); } pstmt.executeBatch(); con.setAutoCommit(true);

于 2014-03-18T13:42:07.877 回答
-2

这不能回答您的问题,但它确实显示了如何避免为每次迭代创建准备好的语句。相反,您可以准备一次,然后在每次迭代中绑定不同的值。

public void insertValues(Connection con, ResultSet rs) {
    try {

        StringBuffer insert_into_temp = new StringBuffer();

        ResultSetMetaData rsmd = rs.getMetaData();
        int colCount = rsmd.getColumnCount();

        insert_into_temp.append("INSERT INTO SESSION.RETURNED_TICKETS (");

        for (int i = 1; i <= colCount; i++) {
            insert_into_temp.append(rsmd.getColumnLabel(i)); 
            insert_into_temp.append(",");
        }

        insert_into_temp.deleteCharAt(insert_into_temp.length()-1);
        insert_into_temp.append(")");

        insert_into_temp.append("\nVALUES(");

        // number of place-holders for values
        for (int i = 0; i < colCount; i++) {
             insert_into_temp.append("?,");  
        }
        insert_into_temp.deleteCharAt(insert_into_temp.length()-1);
        insert_into_temp.append(")");

        PreparedStatement pstmt = con.prepareStatement(insert_into_temp.toString());

        while (rs.next()) {

            pstmt.setInt(1, rs.getInt(Ticket.FLD_ID));
            pstmt.setString(2, rs.getString(Ticket.FLD_DESCRIPTION));
            pstmt.setInt(3, rs.getInt(Ticket.FLD_TICKETTYPE));
            pstmt.setString(4, rs.getString("STATE"));
            pstmt.setString(5, rs.getString("PRIORITY"));
            pstmt.setString(6, rs.getString("OWNER"));
            pstmt.setString(7, rs.getString("SUBMITTER"));
            pstmt.setString(8, rs.getString("TYPE"));
            pstmt.setString(9, rs.getString(Ticket.FLD_TITLE));
            pstmt.setString(10, rs.getString("PROJECT"));
            pstmt.setInt(11, rs.getInt("PROJID"));
            pstmt.setDouble(12, rs.getDouble("RELEASE"));
            pstmt.setTimestamp(13, rs.getTimestamp(Ticket.FLD_SUBMITDATE));
            pstmt.setInt(14, rs.getInt(Ticket.FLD_CUSTOMER));  
            pstmt.setInt(15, rs.getInt("ROW_NEXT")); 

            int success = pstmt.executeUpdate();

            if (success != 1) // if not successful
                throw new SQLException("Failed to insert values into temporary table for linked/unlinked tickets");

        }

    } catch (SQLException e){
        LogFile.logError("[Report.execute()] "+e.getMessage());
        LogFile.logError(insert_into_temp.toString());
    }
}
于 2013-07-03T12:16:58.813 回答