rwinkler
asked on
ResultSet persistence
I need some way to maintain data from a ResultSet after closing the connection. In other words, I want something that can get the data from the database, close the connection, then return the results. If it helps, I'm using an Oracle 8 database. Any suggestions?
//psuedocode
private ResultSet mymethod(String sql)
{
Connection conn = getConnection();
Statement stmt = conn.getStatement();
ResultSet rs = stmt.execute(sql);
conn.close();
return rs;
}
//psuedocode
private ResultSet mymethod(String sql)
{
Connection conn = getConnection();
Statement stmt = conn.getStatement();
ResultSet rs = stmt.execute(sql);
conn.close();
return rs;
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
and http://www.orionserver.com/docs/jdbc/javax/sql/CachedRowSet.html
See the RsultSet object must be associated with a valid statement and of course connection object.. by closing connection mean you closed the resultset object..
Just Change Ur Function A little and using ResultSetMetaData before closing your connection put all the data in an sutiable Collection or Map object and return that ArrayList or whatever Collection object you choose .. this way ya would be having Results of Query but ofcourse.. not in the form of ResultSet but another COllection object
Just Change Ur Function A little and using ResultSetMetaData before closing your connection put all the data in an sutiable Collection or Map object and return that ArrayList or whatever Collection object you choose .. this way ya would be having Results of Query but ofcourse.. not in the form of ResultSet but another COllection object
ASKER
EXACTLY what I was looking for! Thanks!
For archival purposes:
CachedRowSet crs = new CachedRowSet();
Listing 1: Explicit CachedRowSet initialization
<jsp:useBean id="Contacts"
class="sun.jdbc.rowset.Cac hedRowSet"
scope="session">
<%
// load database driver
Class.forName("sun.jdbc.od bc.JdbcOdb cDriver");
// initialize our CachedRowSet bean
Contacts.setUsername("dbus er"); // example userid
Contacts.setPassword("dbpa ssword"); // example password
Contacts.setUrl("jdbc:odbc :ContactDB "); // example DSN
Contacts.setCommand("SELEC T name, telephone from Contacts");
Contacts.execute();
%>
</jsp:useBean>
OR
<%
// initialize our CachedRowSet bean using dataSourceName
// property
Contacts.setDataSourceName ("Database s/Contacts DB/Datasou rce");
Contacts.setCommand("SELEC T name, telephone from Contacts");
Contacts.execute();
%>
OR
<%
// get connection from pool
InitialContext ctx = new InitialContext();
javax.sql.DataSource ds =
(javax.sql.DataSource)ctx. lookup
("Databases/ContactsDB/Dat aSource");
java.sql.Connection con = ds.getConnection();
Contacts.setCommand("SELEC T name, telephone from Contacts");
// supply the connection to the RowSet
Contacts.execute(con);
%>
For archival purposes:
CachedRowSet crs = new CachedRowSet();
Listing 1: Explicit CachedRowSet initialization
<jsp:useBean id="Contacts"
class="sun.jdbc.rowset.Cac
scope="session">
<%
// load database driver
Class.forName("sun.jdbc.od
// initialize our CachedRowSet bean
Contacts.setUsername("dbus
Contacts.setPassword("dbpa
Contacts.setUrl("jdbc:odbc
Contacts.setCommand("SELEC
Contacts.execute();
%>
</jsp:useBean>
OR
<%
// initialize our CachedRowSet bean using dataSourceName
// property
Contacts.setDataSourceName
Contacts.setCommand("SELEC
Contacts.execute();
%>
OR
<%
// get connection from pool
InitialContext ctx = new InitialContext();
javax.sql.DataSource ds =
(javax.sql.DataSource)ctx.
("Databases/ContactsDB/Dat
java.sql.Connection con = ds.getConnection();
Contacts.setCommand("SELEC
// supply the connection to the RowSet
Contacts.execute(con);
%>