-
Bug
-
Resolution: Not an Issue
-
P4
-
None
-
6
-
generic
-
generic
A DESCRIPTION OF THE FIX :
Bug Description : Make method getRowCount java.sql.ResultSet public.
In sun.jdbc.odbc.JdbcOdbcResultSet method getRowCount is public. Why not offer this method in java.sql.ResultSet.
Workaround:
public int getRowCount(ResultSet rs) throws SQLException {
boolean wasLast = rs.isLast();
int markedPosition = 0;
if ( !wasLast) {
markedPosition = rs.isAfterLast() ? -1 : rs.getRow();
// rs.absolute( -1); // causes bug: refresh() and cancelUpdates() don't work anymore
// Next line to avoid SQLException (tested on MS-Access DB)
if (myMarkedPosition == 0) rs.afterLast();
rs.last();
}
int rowCount = rs.getRow();
if ( !wasLast) {
if (markedPosition == 0) rs.beforeFirst();
else if (markedPosition == -1) rs.afterLast();
else rs.absolute( markedPosition);
}
return rowCount;
}
Subset of Releases affected : Mustang.
Platforms affected : all
Test case : See Test case section.
Diff baseline : 6.0 Beta
Diff :
--- C:\Programme\Java\jdk1.6.0\src\j2se\src\share\classes\java\sql\ResultSet.java 2006-03-16 18:06:58.767398400 +0100
+++ ..\src\java\sql\ResultSet.java 2006-03-23 20:15:09.055424000 +0100
@@ -3,6 +3,8 @@
*
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
+ *
+ * Contributors: Ulf Zibis, CoSoCo, Germany
*/
package java.sql;
@@ -2431,9 +2433,19 @@
* @since 1.4
*/
void updateArray(String columnName, java.sql.Array x) throws SQLException;
-
- //------------------------- JDBC 4.0 -----------------------------------
+ //------------------------- JDBC 4.0 -----------------------------------
+
+ /**
+ * Retrieves the total number of rows of this <code>ResultSet</code> object.
+ * Note that on some databases this method may be very expensive.
+ *
+ * @return the total number of rows
+ * @throws SQLException if a database access error occurs
+ * @since 1.6
+ */
+ int getRowCount() throws SQLException;
+
/**
* Retrieves the value of the designated column in the current row of this
* <code>ResultSet</code> object as a <code>java.sql.RowId</code> object in the Java
JUnit TESTCASE :
/*
* ResultSetTest.java
* JUnit based test
*
* Created on 23. März 2006, 19:35
*
*/
package java.sql;
/**
*
* @author Ulf Zibis
*/
public class ResultSetTest extends TestCase {
private final static String URL = "jdbc:odbc:TestData";
private final static String TABLE = "Test1";
private Connection testData;
public ResultSetTest(String testName) {
super(testName);
}
protected void setUp() throws Exception {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
testData = DriverManager.getConnection( URL);
Statement stmt1 = testData.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
ResultSet rs1 = stmt1.executeQuery("SELECT * FROM "+TABLE);
}
catch (SQLException se) {
System.out.println( se+", Error: "+se.getErrorCode()+", SQLState: "+se.getSQLState());
se.printStackTrace();
}
catch (Throwable t) {
t.printStackTrace();
}
}
protected void tearDown() throws Exception {
try { if (testData != null) testData.close(); }
catch (Throwable t) { t.printStackTrace(); }
}
public static Test suite() {
TestSuite suite = new TestSuite(ResultSetTest.class);
return suite;
}
public void testGetRowCount() {
System.out.println("test java.sql.ResultSet#getRowCount() ...");
int expResult;
int result;
try {
Statement stmt = testData.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
ResultSet rs = stmt.executeQuery("SELECT * FROM "+TABLE);
result = rs.getRowCount();
rs.last();
expResult = rs.getRow();
}
catch (SQLException se) {
System.out.println( se+", Error: "+se.getErrorCode()+", SQLState: "+se.getSQLState());
se.printStackTrace();
}
catch (Throwable t) {
t.printStackTrace();
}
assertEquals(expResult, result);
}
}
Bug Description : Make method getRowCount java.sql.ResultSet public.
In sun.jdbc.odbc.JdbcOdbcResultSet method getRowCount is public. Why not offer this method in java.sql.ResultSet.
Workaround:
public int getRowCount(ResultSet rs) throws SQLException {
boolean wasLast = rs.isLast();
int markedPosition = 0;
if ( !wasLast) {
markedPosition = rs.isAfterLast() ? -1 : rs.getRow();
// rs.absolute( -1); // causes bug: refresh() and cancelUpdates() don't work anymore
// Next line to avoid SQLException (tested on MS-Access DB)
if (myMarkedPosition == 0) rs.afterLast();
rs.last();
}
int rowCount = rs.getRow();
if ( !wasLast) {
if (markedPosition == 0) rs.beforeFirst();
else if (markedPosition == -1) rs.afterLast();
else rs.absolute( markedPosition);
}
return rowCount;
}
Subset of Releases affected : Mustang.
Platforms affected : all
Test case : See Test case section.
Diff baseline : 6.0 Beta
Diff :
--- C:\Programme\Java\jdk1.6.0\src\j2se\src\share\classes\java\sql\ResultSet.java 2006-03-16 18:06:58.767398400 +0100
+++ ..\src\java\sql\ResultSet.java 2006-03-23 20:15:09.055424000 +0100
@@ -3,6 +3,8 @@
*
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
+ *
+ * Contributors: Ulf Zibis, CoSoCo, Germany
*/
package java.sql;
@@ -2431,9 +2433,19 @@
* @since 1.4
*/
void updateArray(String columnName, java.sql.Array x) throws SQLException;
-
- //------------------------- JDBC 4.0 -----------------------------------
+ //------------------------- JDBC 4.0 -----------------------------------
+
+ /**
+ * Retrieves the total number of rows of this <code>ResultSet</code> object.
+ * Note that on some databases this method may be very expensive.
+ *
+ * @return the total number of rows
+ * @throws SQLException if a database access error occurs
+ * @since 1.6
+ */
+ int getRowCount() throws SQLException;
+
/**
* Retrieves the value of the designated column in the current row of this
* <code>ResultSet</code> object as a <code>java.sql.RowId</code> object in the Java
JUnit TESTCASE :
/*
* ResultSetTest.java
* JUnit based test
*
* Created on 23. März 2006, 19:35
*
*/
package java.sql;
/**
*
* @author Ulf Zibis
*/
public class ResultSetTest extends TestCase {
private final static String URL = "jdbc:odbc:TestData";
private final static String TABLE = "Test1";
private Connection testData;
public ResultSetTest(String testName) {
super(testName);
}
protected void setUp() throws Exception {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
testData = DriverManager.getConnection( URL);
Statement stmt1 = testData.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
ResultSet rs1 = stmt1.executeQuery("SELECT * FROM "+TABLE);
}
catch (SQLException se) {
System.out.println( se+", Error: "+se.getErrorCode()+", SQLState: "+se.getSQLState());
se.printStackTrace();
}
catch (Throwable t) {
t.printStackTrace();
}
}
protected void tearDown() throws Exception {
try { if (testData != null) testData.close(); }
catch (Throwable t) { t.printStackTrace(); }
}
public static Test suite() {
TestSuite suite = new TestSuite(ResultSetTest.class);
return suite;
}
public void testGetRowCount() {
System.out.println("test java.sql.ResultSet#getRowCount() ...");
int expResult;
int result;
try {
Statement stmt = testData.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
ResultSet rs = stmt.executeQuery("SELECT * FROM "+TABLE);
result = rs.getRowCount();
rs.last();
expResult = rs.getRow();
}
catch (SQLException se) {
System.out.println( se+", Error: "+se.getErrorCode()+", SQLState: "+se.getSQLState());
se.printStackTrace();
}
catch (Throwable t) {
t.printStackTrace();
}
assertEquals(expResult, result);
}
}