aspose file tools*
The moose likes JDBC and the fly likes Problems with functions and absolute cursor control. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Problems with functions and absolute cursor control." Watch "Problems with functions and absolute cursor control." New topic
Author

Problems with functions and absolute cursor control.

Clay Graham
Greenhorn

Joined: Dec 01, 2003
Posts: 3
I am attempting to write a paginated results class, which will allow a web user to make paginated queries to the preparedCall and return a OracleTypes.CURSOR object. The user session would then store the current position and only retrieve results associated with the current page using the absolute function. This is basic stuff, I know, but I cant find out how to do it.
We are using the good'ol classes12.zip thin driver with Oracle8i and JDK1.4, which would *seem* to me to support the JDBC2.0 functionality of rs.absolute(2), but I am getting an error: Invalid operation for forward only resultset : absolute which seems to me to be a JDBC 1.0 issue. wierd.
Here is the base code:
CREATE OR REPLACE FUNCTION "GETDOCNUM" (v_title IN VARCHAR2)
RETURN Types.ref_cursor
AS
title_cursor types.ref_cursor;
title_search varchar2(200);
v_query varchar2(500);
ref_title varchar2(500);
ref_docnum varchar2(32);
BEGIN
title_search :='%' || v_title || '%';
v_query:='SELECT TITLE1, DOCUMENT_NUMBER FROM references where title1 like :1 ';
dbms_output.put_line(v_query);
OPEN title_cursor FOR
v_query using title_search;

RETURN title_cursor;
END;
/

import oracle.jdbc.OracleTypes;
import java.util.*;
import java.sql.*;
import javax.sql.*;
/**
*
* @author cwgraham
*/
public class TestStoredProc {
/** Creates a new instance of TestStoredProc */
public TestStoredProc() {
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
try{
Connection conn = null;
String JDBC_CONNECTION_STRING="jdbc racle:thin:@host:1521:sid";
String username= "user";
String password= "password";
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection(JDBC_CONNECTION_STRING,username,password);
if(conn.getMetaData().supportsResultSetConcurrency(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE))
{
System.out.println("options concurrency are supported");
}
if(conn.getMetaData().supportsResultSetType(ResultSet.TYPE_SCROLL_INSENSITIVE)) {
System.out.println("options type are supported");
}
String query="{ call ? := getdocnum(?) }";
CallableStatement stmt = conn.prepareCall(query, java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE, java.sql.ResultSet.CONCUR_READ_ONLY);
// register the type of the out param - an Oracle specific type
stmt.registerOutParameter(1, OracleTypes.CURSOR);
// set the in param
stmt.setString(2,"AFRICANS"); //title
// execute and retrieve the result set
stmt.execute();
ResultSet rs = (ResultSet)stmt.getObject(1);
int start=4;
int end=8;
//int i=1;
for(int i=start; i<=end; i++)
{
rs.absolute(i);
//rs.next();
System.out.println("#"+i+":"+rs.getString("DOCUMENT_NUMBER")+": "+rs.getString("TITLE1"));
}
conn.close();
}
catch(ClassNotFoundException cnfex)
{
System.out.println("error:"+cnfex.getMessage());
}
catch(SQLException sex)
{
System.out.println("error:"+sex.getMessage());
}
}
}
So it responds as if it was JDBC 1.0, even though I have verified that it
supports cursor control:
options concurrency are supported
options type are supported
Invalid operation for forward only resultset : absolute
So I would love to solve this problem, at least I would have a first step to a solution, but the next question is: shouldn't there be an even better way? It would seem to me in this web world of ConnectionPools and web applications with session, that we could close the connection, save some sort of session id in the web application session state, and come back to this cursor using some type of oracle session id when we open the connection. Do you know how this may be possible?
Avi Abrami
Ranch Hand

Joined: Oct 11, 2000
Posts: 1135

Clay,
I don't remember where I read it (but I guess it was in the Oracle documentation :-), but a REF CURSOR can only provide a "forward only result set". Also, for regular "ResultSet"s (obtained via SQL queries -- and not REF CURSORs), not every query can produce a scrollable result set. In those cases, Oracle will (quietly ;-) force the "ResultSet" to be forward only. Again, details can be found in the Oracle documentation.
If you want to write a "paginated results class", I suggest you read the discussion entitled getting rows N through M of a result set from the Ask Tom Web site (assuming you haven't already)
Good Luck,
Avi.
Clay Graham
Greenhorn

Joined: Dec 01, 2003
Posts: 3
SOLUTION:

The fundamental basis of this solution is the following query:
SELECT
TMP.RN,
TMP.TITLE1,
TMP.DOCUMENT_NUMBER
FROM
(
SELECT
ROWNUM RN,
TITLE1,
DOCUMENT_NUMBER
FROM
REFERENCES
WHERE
TITLE1 LIKE '%BLOOD%'
) TMP
WHERE
TMP.RN BETWEEN 41 AND 60;
once you see that works the following solution comes out of it:
CREATE OR REPLACE FUNCTION "GETTITLEREFCOUNT" (v_title IN VARCHAR2)
RETURN INTEGER
IS
rowcount INTEGER;
BEGIN
select Count(ROWID) TOTAL into rowcount from REFERENCES where
TITLE1 LIKE '%' || v_title || '%';

RETURN rowcount;
END GETTITLEREFCOUNT;
--------------------------------------------------------------------------
-
CREATE OR REPLACE FUNCTION "GETDOCNUM" (
v_title IN VARCHAR2,
v_startrow IN INTEGER,
v_endrow IN INTEGER
)
RETURN Types.ref_cursor
AS
title_cursor types.ref_cursor;
title_search varchar2(200);
v_query varchar2(500);
BEGIN
title_search :='%' || v_title || '%';
v_query:='SELECT TMP.RN, TMP.TITLE1, TMP.DOCUMENT_NUMBER FROM
(SELECT ROWNUM RN, TITLE1, DOCUMENT_NUMBER FROM references where TITLE1
like :1) TMP WHERE TMP.RN BETWEEN '|| v_startrow ||' and ' || v_endrow;
dbms_output.put_line(v_query);

OPEN title_cursor FOR
v_query using title_search;


RETURN title_cursor;

END;
--------------------------------------------------------------------------
--
import oracle.jdbc.OracleTypes;
import java.util.*;
import java.sql.*;
import javax.sql.*;
/**
*
* @author cwgraham
*/
public class TestStoredProc {

private static final int PAGESIZE = 20;
private static final int PAGENUMBER = 3;
private static final String TITLESEARCH = "BLOOD";



/** Creates a new instance of TestStoredProc */
public TestStoredProc() {
}

private Connection makeDBConnection() throws SQLException,
ClassNotFoundException
{
String
JDBC_CONNECTION_STRING="jdbc racle:thin:@host:1521:km01";
String username= "user";
String password= "xxxxx";
Class.forName("oracle.jdbc.driver.OracleDriver");
return
DriverManager.getConnection(JDBC_CONNECTION_STRING,username,password);
}


public int getRowcount(String title, Connection conn) throws
SQLException
{
String query="{ call ? := gettitlerefcount(?) }";
CallableStatement stmt = conn.prepareCall(query,
java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,
java.sql.ResultSet.CONCUR_READ_ONLY);
// register the type of the out param - an Oracle specific type
stmt.registerOutParameter(1, OracleTypes.INTEGER);
// set the in param
stmt.setString(2, title); //title
// execute and retrieve the result set
stmt.execute();
return stmt.getInt(1);
}

public int calcStartPos(int pagenum, int recordcount, int pagesize)
{
return (pagesize*(pagenum-1))+1;
}


public int calcEndPos(int pagenum, int recordcount, int pagesize)
{
return (pagesize*pagenum);
}
public ResultSet getPage(int pagenum, int recordcount, int pagesize,
String title, Connection conn) throws SQLException
{
String query="{ call ? := getdocnum(?,?,?) }";
CallableStatement stmt = conn.prepareCall(query,
java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,
java.sql.ResultSet.CONCUR_READ_ONLY);
// register the type of the out param - an Oracle specific
type
stmt.registerOutParameter(1, OracleTypes.CURSOR);
// set the in param
stmt.setString(2,title); //title

stmt.setInt(3,this.calcStartPos(pagenum,recordcount,pagesize)); //title
stmt.setInt(4,this.calcEndPos(pagenum,recordcount,pagesize));
//title
// execute and retrieve the result set
stmt.execute();
return (ResultSet)stmt.getObject(1);

}

private void displayResults(ResultSet rs, int start) throws
SQLException
{
while(rs.next())
{

System.out.println("#"+start+":"+rs.getString("DOCUMENT_NUMBER")+":
"+rs.getString("TITLE1"));
start++;
}

}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
TestStoredProc sptest = new TestStoredProc();

try{

Connection conn = sptest.makeDBConnection();

if(conn.getMetaData().supportsResultSetConcurrency(ResultSet.TYPE_SCROLL_I
NSENSITIVE, ResultSet.CONCUR_UPDATABLE))
{
System.out.println("options concurrency are
supported");
}


if(conn.getMetaData().supportsResultSetType(ResultSet.TYPE_SCROLL_INSENSIT
IVE)) {
System.out.println("options type are supported");
}
int count = sptest.getRowcount(TITLESEARCH, conn);
System.out.println("found "+count+" records
containing:"+TITLESEARCH);
System.out.println("starting record for page
#"+PAGENUMBER+"="+sptest.calcStartPos(PAGENUMBER,count,PAGESIZE));
System.out.println("ending record for page
#"+PAGENUMBER+"="+sptest.calcEndPos(PAGENUMBER,count,PAGESIZE));

ResultSet rs = sptest.getPage(PAGENUMBER, count, PAGESIZE,
TITLESEARCH, conn);
sptest.displayResults(rs,
sptest.calcStartPos(PAGENUMBER,count,PAGESIZE));

conn.close();

}
catch(ClassNotFoundException cnfex)
{
System.out.println("error:"+cnfex.getMessage());
}
catch(SQLException sex)
{
System.out.println("error:"+sex.getMessage());
}

}

}
---------------------------------------------------
[ December 03, 2003: Message edited by: Clay Graham ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problems with functions and absolute cursor control.