我有一个返回50行的ResultSet。我需要有一个临时表,我插入这50行,以便我可以对它执行查询。
没有替代方案,所以请不要建议使用子查询或其他内容。需要临时表。
所以我使用以下方法插入行,显然虽然我知道ResultSet由50行组成,但它在while循环中仅循环13次,因此当我从这个表中提取一些字段时,我没有所需的结果。
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());
}
}
可能是什么问题?我无法弄清楚为什么会这样。 谢谢
答案 0 :(得分:0)
如果我正确理解了问题,而不是每次都像这样执行查询,
con.setAutoCommit(false);
while (rs.next()) {
// your setting values on prepared statement code
pstmt.addBatch();
}
pstmt.executeBatch();
con.setAutoCommit(true);
答案 1 :(得分:-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());
}
}