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