Re: Resetting ResultSet with first() or beforeFirst()
Kristian: thank you for being so observant. Actually, it was a programmatic typo on my part, as I just tried to type the pseudo-code into the email. My production code does follow the described pattern: final public Object getValue(ResultSet resultSet) throws SQLException { final Object object = resultSet.getObject(m_columnIdx); if (resultSet.wasNull()) { return null; } return object; } /**/ final Object object = resultSet.getObject(i); String string; if (object == null || resultSet.wasNull()) { string = m_nullValueIndicator; } else { switch (columnTypes[i]) { /**/ while (resultSet.next()) { tableBuilder.append(TR); for (int i = 1; i = columnCount; i++) { final String string = resultSet.getString(i); final String value = (string == null || resultSet.wasNull() ? nbsp; : string.trim()); tableBuilder.append(TD).append(value).append(/TD); } tableBuilder.append(/TR\n); } Much appreciated, Pavel. Kristian Waagan kristian.waa...@sun.com Sent by: kristian.waa...@sun.com 06/10/2010 04:59 AM Please respond to Derby Discussion derby-user@db.apache.org To derby-user@db.apache.org cc Subject Re: Resetting ResultSet with first() or beforeFirst() On 09.06.10 19:32, Pavel Bortnovskiy wrote: Hi: I am trying to reset a given ResultSet back to the first record, so that I could iterate through its records one more time, after it's already been done once. So, my code does something like this (it's pseudo-code, and in the real application these two passes are done in different parts of the application): // get ResultSet final ResultSet resultSet = ((PreparedStatement) m_statement).executeQuery(); final int columnCount = resultSet.getMetaData().getColumnCount(); // First Pass through the data while (resultSet.next()) { for (int i = 1; i = columnCount; i++) { if (resultSet.wasNull()) { Hi, I don't know of this issue is present in your actual code, but note that ResultSet.wasNull must be called *after* a getter method has been called. From the JavaDocs: Reports whether the last column read had a value of SQL NULL. Note that you must first call one of the getter methods on a column to try to read its value and then call the method wasNull to see if the value read was SQL NULL. doSomethingWithNULL(); } else { doSomethingWithObject(resultSet.getObject(i)); Assuming wasNull was called after the getter method, the second getObject above may raise an exception in Derby. The restriction that you can call a getter only once for a given column is limited to some data types (i.e. BLOB and CLOB) and some getters (getXXXStream, getObject for the data types mentioned). Just thought I'd mention it :) Regards, -- Kristian } } } // Reset ResultSet - but it generates SQLException: The 'beforeFirst()' method is only allowed on scroll cursors resultSet.beforeFirst(); // Second Pass through the data while (resultSet.next()) { for (int i = 1; i = columnCount; i++) { if (resultSet.wasNull()) { doSomethingElseWithNULL(); } else { doSomethingElseWithObject(resultSet.getObject(i)); } } } However, resultSet.beforeFirst() generates an exception: java.sql.SQLException: The 'beforeFirst()' method is only allowed on scroll cursors. at org.apache.derby.impl.jdbc.SQLExceptionFactory40.getSQLException(Unknown Source) at org.apache.derby.impl.jdbc.Util.newEmbedSQLException(Unknown Source) at org.apache.derby.impl.jdbc.Util.newEmbedSQLException(Unknown Source) at org.apache.derby.impl.jdbc.EmbedResultSet.checkScrollCursor(Unknown Source) at org.apache.derby.impl.jdbc.EmbedResultSet.beforeFirst(Unknown Source) Caused by: java.sql.SQLException: The 'beforeFirst()' method is only allowed on scroll cursors. at org.apache.derby.impl.jdbc.SQLExceptionFactory.getSQLException(Unknown Source) at org.apache.derby.impl.jdbc.SQLExceptionFactory40.wrapArgsForTransportAcrossDRDA(Unknown Source) ... 8 more Could someone please help me out to understand what may be done incorrectly and what would be the right approach to enable two-pass logic through the RecordSet? Thanks, Pavel. Jefferies archives and monitors outgoing and incoming e-mail. The contents of this email, including any attachments, are confidential to the ordinary user of the email address to which it was addressed. If you are not the addressee of this email you may not copy, forward, disclose or otherwise use it or any part of it in any form whatsoever. This email may be produced at the request of regulators or in connection with civil litigation. Jefferies accepts no liability for any errors or omissions arising as a result
Resetting ResultSet with first() or beforeFirst()
Hi: I am trying to reset a given ResultSet back to the first record, so that I could iterate through its records one more time, after it's already been done once. So, my code does something like this (it's pseudo-code, and in the real application these two passes are done in different parts of the application): // get ResultSet final ResultSet resultSet = ((PreparedStatement) m_statement).executeQuery(); final int columnCount = resultSet.getMetaData().getColumnCount(); // First Pass through the data while (resultSet.next()) { for (int i = 1; i = columnCount; i++) { if (resultSet.wasNull()) { doSomethingWithNULL(); } else { doSomethingWithObject(resultSet.getObject(i)); } } } // Reset ResultSet - but it generates SQLException: The 'beforeFirst()' method is only allowed on scroll cursors resultSet.beforeFirst(); // Second Pass through the data while (resultSet.next()) { for (int i = 1; i = columnCount; i++) { if (resultSet.wasNull()) { doSomethingElseWithNULL(); } else { doSomethingElseWithObject(resultSet.getObject(i)); } } } However, resultSet.beforeFirst() generates an exception: java.sql.SQLException: The 'beforeFirst()' method is only allowed on scroll cursors. at org.apache.derby.impl.jdbc.SQLExceptionFactory40.getSQLException(Unknown Source) at org.apache.derby.impl.jdbc.Util.newEmbedSQLException(Unknown Source) at org.apache.derby.impl.jdbc.Util.newEmbedSQLException(Unknown Source) at org.apache.derby.impl.jdbc.EmbedResultSet.checkScrollCursor(Unknown Source) at org.apache.derby.impl.jdbc.EmbedResultSet.beforeFirst(Unknown Source) Caused by: java.sql.SQLException: The 'beforeFirst()' method is only allowed on scroll cursors. at org.apache.derby.impl.jdbc.SQLExceptionFactory.getSQLException(Unknown Source) at org.apache.derby.impl.jdbc.SQLExceptionFactory40.wrapArgsForTransportAcrossDRDA(Unknown Source) ... 8 more Could someone please help me out to understand what may be done incorrectly and what would be the right approach to enable two-pass logic through the RecordSet? Thanks, Pavel. Jefferies archives and monitors outgoing and incoming e-mail. The contents of this email, including any attachments, are confidential to the ordinary user of the email address to which it was addressed. If you are not the addressee of this email you may not copy, forward, disclose or otherwise use it or any part of it in any form whatsoever. This email may be produced at the request of regulators or in connection with civil litigation. Jefferies accepts no liability for any errors or omissions arising as a result of transmission. Use by other than intended recipients is prohibited. In the United Kingdom, Jefferies operates as Jefferies International Limited; registered in England: no. 1978621; registered office: Vintners Place, 68 Upper Thames Street, London EC4V 3BJ. Jefferies International Limited is authorised and regulated by the Financial Services Authority.
Re: Resetting ResultSet with first() or beforeFirst()
On 06/ 9/10 07:32 PM, Pavel Bortnovskiy wrote: Hi: I am trying to reset a given ResultSet back to the first record, so that I could iterate through its records one more time, after it's already been done once. So, my code does something like this (it's pseudo-code, and in the real application these two passes are done in different parts of the application): // get ResultSet final ResultSet resultSet = ((PreparedStatement) m_statement).executeQuery(); final int columnCount = resultSet.getMetaData().getColumnCount(); // First Pass through the data while (resultSet.next()) { for (int i = 1; i = columnCount; i++) { if (resultSet.wasNull()) { doSomethingWithNULL(); } else { doSomethingWithObject(resultSet.getObject(i)); } } } // Reset ResultSet - but it generates SQLException: The 'beforeFirst()' method is only allowed on scroll cursors resultSet.beforeFirst(); // Second Pass through the data while (resultSet.next()) { for (int i = 1; i = columnCount; i++) { if (resultSet.wasNull()) { doSomethingElseWithNULL(); } else { doSomethingElseWithObject(resultSet.getObject(i)); } } } However, resultSet.beforeFirst() generates an exception: java.sql.SQLException: The 'beforeFirst()' method is only allowed on scroll cursors. at org.apache.derby.impl.jdbc.SQLExceptionFactory40.getSQLException(Unknown Source) at org.apache.derby.impl.jdbc.Util.newEmbedSQLException(Unknown Source) at org.apache.derby.impl.jdbc.Util.newEmbedSQLException(Unknown Source) at org.apache.derby.impl.jdbc.EmbedResultSet.checkScrollCursor(Unknown Source) at org.apache.derby.impl.jdbc.EmbedResultSet.beforeFirst(Unknown Source) Caused by: java.sql.SQLException: The 'beforeFirst()' method is only allowed on scroll cursors. at org.apache.derby.impl.jdbc.SQLExceptionFactory.getSQLException(Unknown Source) at org.apache.derby.impl.jdbc.SQLExceptionFactory40.wrapArgsForTransportAcrossDRDA(Unknown Source) ... 8 more Could someone please help me out to understand what may be done incorrectly and what would be the right approach to enable two-pass logic through the RecordSet? You need to specify that the result should be scrollable when you prepare the statement. Something like this: PreparedStatement ps = conn.prepareStatement( sql, |ResultSet.TYPE_SCROLL_INSENSITIVE, ||ResultSet.CONCUR_READ_ONLY);| -- Knut Anders
Re: Resetting ResultSet with first() or beforeFirst()
Thank you, Knut. Is there a large performance penalty for specifying ResultSet.TYPE_SCROLL_INSENSITIVE, ||ResultSet.CONCUR_READ_ONLY? For the sake of efficiency, should my code set those only when it is necessary to reset the ResultSet? Pavel. Knut Anders Hatlen knut.hat...@sun.com Sent by: knut.hat...@sun.com 06/09/2010 06:21 PM Please respond to Derby Discussion derby-user@db.apache.org To derby-user@db.apache.org cc Subject Re: Resetting ResultSet with first() or beforeFirst() On 06/ 9/10 07:32 PM, Pavel Bortnovskiy wrote: Hi: I am trying to reset a given ResultSet back to the first record, so that I could iterate through its records one more time, after it's already been done once. So, my code does something like this (it's pseudo-code, and in the real application these two passes are done in different parts of the application): // get ResultSet final ResultSet resultSet = ((PreparedStatement) m_statement).executeQuery(); final int columnCount = resultSet.getMetaData().getColumnCount(); // First Pass through the data while (resultSet.next()) { for (int i = 1; i = columnCount; i++) { if (resultSet.wasNull()) { doSomethingWithNULL(); } else { doSomethingWithObject(resultSet.getObject(i)); } } } // Reset ResultSet - but it generates SQLException: The 'beforeFirst()' method is only allowed on scroll cursors resultSet.beforeFirst(); // Second Pass through the data while (resultSet.next()) { for (int i = 1; i = columnCount; i++) { if (resultSet.wasNull()) { doSomethingElseWithNULL(); } else { doSomethingElseWithObject(resultSet.getObject(i)); } } } However, resultSet.beforeFirst() generates an exception: java.sql.SQLException: The 'beforeFirst()' method is only allowed on scroll cursors. at org.apache.derby.impl.jdbc.SQLExceptionFactory40.getSQLException(Unknown Source) at org.apache.derby.impl.jdbc.Util.newEmbedSQLException(Unknown Source) at org.apache.derby.impl.jdbc.Util.newEmbedSQLException(Unknown Source) at org.apache.derby.impl.jdbc.EmbedResultSet.checkScrollCursor(Unknown Source) at org.apache.derby.impl.jdbc.EmbedResultSet.beforeFirst(Unknown Source) Caused by: java.sql.SQLException: The 'beforeFirst()' method is only allowed on scroll cursors. at org.apache.derby.impl.jdbc.SQLExceptionFactory.getSQLException(Unknown Source) at org.apache.derby.impl.jdbc.SQLExceptionFactory40.wrapArgsForTransportAcrossDRDA(Unknown Source) ... 8 more Could someone please help me out to understand what may be done incorrectly and what would be the right approach to enable two-pass logic through the RecordSet? You need to specify that the result should be scrollable when you prepare the statement. Something like this: PreparedStatement ps = conn.prepareStatement( sql, |ResultSet.TYPE_SCROLL_INSENSITIVE, ||ResultSet.CONCUR_READ_ONLY);| -- Knut Anders Jefferies archives and monitors outgoing and incoming e-mail. The contents of this email, including any attachments, are confidential to the ordinary user of the email address to which it was addressed. If you are not the addressee of this email you may not copy, forward, disclose or otherwise use it or any part of it in any form whatsoever. This email may be produced at the request of regulators or in connection with civil litigation. Jefferies accepts no liability for any errors or omissions arising as a result of transmission. Use by other than intended recipients is prohibited. In the United Kingdom, Jefferies operates as Jefferies International Limited; registered in England: no. 1978621; registered office: Vintners Place, 68 Upper Thames Street, London EC4V 3BJ. Jefferies International Limited is authorised and regulated by the Financial Services Authority.