Hi folks,
Just downloaded Sun's Early Access implementation of JDBC CachedRowset
(Rowset) [3]. I am trying to create (manufacture) a CachedRowset without
any direct links to a backend database. These are my steps:
i. Create a RowsetMetaData object (which defines the fields/columns etc);
ii. Instantiate a CachedRowset and set the metadata property to i. above;
iii. Set some CachedRowset properties that will allow for updating;
iv. Populate the CachedRowset
I have succeeded (or it seems) with the above, but I fail to scroll thru the
populated CachedRowset (to get the data out). The error I get is:
java.sql.SQLException: Invalid Cursor position
at com.sun.rowset.CachedRowSetImpl.next(CachedRowSetImpl.java:1201)
at
com.capitalalliance.integration.universe.common.TestCachedRowset.main(TestCa
chedRowset.java:51)
The execution fails on crs.next().
What am I missing, any ideas?
Many thanks in advance,
Regards
S Kauchali
References:
[1]
http://java.sun.com/developer/technicalArticles/javaserverpages/cachedrowset/
[2] http://java.sun.com/developer/Books/JDBCTutorial/chapter5.html
Download:
[3] http://java.sun.com/developer/earlyAccess/jdbc/jdbc-rowset.html
Platform details:
JDK 1.4.2_04
Windows XP Pro
////////////////////code///////////////////
/*
* Created on Mar 29, 2004
*
* To change the template for this generated file go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
package com.capitalalliance.integration.universe.common;
/**
* @author Shuaib Kauchali
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
import javax.sql.rowset.*;
import javax.sql.*;
import com.sun.rowset.*;
import java.sql.*;
public class TestCachedRowset {
public static void main(String[] args) {
try {
RowSetMetaData rowMeta = new RowSetMetaDataImpl();
rowMeta.setColumnCount(49);
for (int i = 1; i < 50; i++) {
rowMeta.setColumnName(i, "columnno" + String.valueOf(i));
rowMeta.setColumnType(i, 12);
i++;
}
CachedRowSet crs = new CachedRowSetImpl();
crs.setFetchDirection(ResultSet.FETCH_FORWARD);
crs.setType(ResultSet.TYPE_SCROLL_INSENSITIVE);
crs.setConcurrency(ResultSet.CONCUR_UPDATABLE);
crs.setTableName("MyTable");
crs.setMetaData(rowMeta);
int j=1;
for (int k = 1; k < 10; k++) {
crs.moveToInsertRow();
for (j = 1; j < 50; j++) {
crs.updateString(j, "value isss =" + j + k);
}
crs.insertRow();
}
while (crs.next()) { //<-- bombs out here
System.out.println(crs.getString(1));
System.out.println(crs.getString(2));
System.out.println(crs.getString(3));
System.out.println(crs.getString(4));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Abdullah Kauchali - 30 Mar 2004 15:03 GMT
Ok, found out what the problem was:
I seemed to hit "crs.moveToInsertRow();" several times (as it was in the for
loop). I should only be calling crs.moveToInsertRow() once!
So, the code at the end of this post works.
Hope it helps anyone in the same predicament.
Cheers
//////////////code////////////////
/*
* Created on Mar 29, 2004
*
* To change the template for this generated file go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
package com.capitalalliance.integration.universe.common;
/**
* @author Shuaib Kauchali
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
import javax.sql.rowset.*;
import javax.sql.*;
import com.sun.rowset.*;
import java.sql.*;
public class TestCachedRowset {
public static void main(String[] args) {
try {
RowSetMetaData rowMeta = new RowSetMetaDataImpl();
rowMeta.setColumnCount(49);
for (int i = 1; i < 50; i++) {
rowMeta.setColumnName(i, "columnno" + String.valueOf(i));
rowMeta.setColumnType(i, 12);
i++;
}
CachedRowSet crs = new CachedRowSetImpl();
crs.setTableName("MyTable");
crs.setMetaData(rowMeta);
crs.setFetchDirection(ResultSet.FETCH_FORWARD);
crs.setType(ResultSet.TYPE_SCROLL_INSENSITIVE);
crs.setConcurrency(ResultSet.CONCUR_UPDATABLE);
int j = 1;
crs.moveToInsertRow();
for (int k = 1; k < 10; k++) {
for (j = 1; j < 50; j++) {
crs.updateString(j, "value isss =" + j + k);
}
crs.insertRow();
}
crs.moveToCurrentRow();
System.out.println(crs.size());
while (crs.next()) {
System.out.println(crs.getString(1));
System.out.println(crs.getString(2));
System.out.println(crs.getString(3));
System.out.println(crs.getString(4));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}