SQL Cursors HELP ASAP

I need somebody to help me with sql cursors, in JSP.

This is my peace of code what is wrong with it?

Statement stmt = myConn.createStatement();

stmt.executeQuery("BEGIN WORK");

stmt.executeQuery("DECLARE item_cursor CURSOR FOR SELECT user_name FROM admin_info");

stmt.executeQuery("FETCH 10 FROM item_cursor");

ResultSet rs = stmt.getResultSet();

while(rs.next()){

if(rs.getString(1) != null){

user_name = rs.getString(1).trim();

}

%><P><%= user_name %></P><%

}

stmt.executeQuery("CLOSE item_cursor");

stmt.executeQuery("COMMIT WORK");

and this is the error that a get: No results where returned by the query

Please help anybody

thanx guys

[800 byte] By [osilekpl] at [2007-9-26 3:10:23]
# 1
Several things...-I am surprised some of that didn't generate exceptions.-Why are you wrapping a query in a transaction?-Why won't just 'select user_name from admin_info' work?
jschell at 2007-6-29 11:16:52 > top of Java-index,Archived Forums,Java Programming...
# 2
JDBC 2.0 supports scrollable resultset.U can use the resultset for setting ur cursors.Check it out!!!Pramod
pramod_bs at 2007-6-29 11:16:52 > top of Java-index,Archived Forums,Java Programming...
# 3
I never done cursors, and I'm asking for some help guys if any of you guys have a pice of code that I can look at that will be great.thanx
osilekpl at 2007-6-29 11:16:52 > top of Java-index,Archived Forums,Java Programming...
# 4

If you are using ORACLE drivers and classes.

This sample program shows Oracle JDBC REF CURSOR functionality, creating a PL/SQL package that includes a stored function that returns a REF CURSOR type. The sample retrieves the REF CURSOR into a result set object.

/*

* This sample shows how to call a PL/SQL function that opens

* a cursor and get the cursor back as a Java ResultSet.

*/

import java.sql.*;

import java.io.*;

import oracle.jdbc.driver.*;

class RefCursorExample

{

public static void main (String args [])

throws SQLException

{

// Load the driver

DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());

// Connect to the database

// You can put a database name after the @ sign in the connection URL.

Connection conn =

DriverManager.getConnection ("jdbc:oracle:oci8:@", "scott", "tiger");

// Create the stored procedure

init (conn);

// Prepare a PL/SQL call

CallableStatement call =

conn.prepareCall ("{ ? = call java_refcursor.job_listing (?)}");

// Find out all the SALES person

call.registerOutParameter (1, OracleTypes.CURSOR);

call.setString (2, "SALESMAN");

call.execute ();

ResultSet rset = (ResultSet)call.getObject (1);

// Dump the cursor

while (rset.next ())

System.out.println (rset.getString ("ENAME"));

// Close all the resources

rset.close();

call.close();

conn.close();

}

// Utility function to create the stored procedure

static void init (Connection conn)

throws SQLException

{

Statement stmt = conn.createStatement ();

stmt.execute ("create or replace package java_refcursor as " +

" type myrctype is ref cursor return EMP%ROWTYPE; " +

" function job_listing (j varchar2) return myrctype; " +

"end java_refcursor;");

stmt.execute ("create or replace package body java_refcursor as " +

" function job_listing (j varchar2) return myrctype is " +

"rc myrctype; " +

" begin " +

"open rc for select * from emp where job = j; " +

"return rc; " +

" end; " +

"end java_refcursor;");

stmt.close();

}

}

ajdiaz at 2007-6-29 11:16:52 > top of Java-index,Archived Forums,Java Programming...
# 5
This is not helping me, im using postgres database.can i get some help.
osilekpl at 2007-6-29 11:16:52 > top of Java-index,Archived Forums,Java Programming...
# 6
Is anybody have any idea how to help me?
osilekpl at 2007-6-29 11:16:52 > top of Java-index,Archived Forums,Java Programming...
# 7

I can't help you until you answer my question.

Why won't just 'select user_name from admin_info' work?

That is a single SQL statement rather than the 8+ that you have.

According to my reference that is valid SQL syntax for PostgresSQL. So you need to provide information why that single statement will not work.

jschell at 2007-6-29 11:16:52 > top of Java-index,Archived Forums,Java Programming...
# 8
I'm able to execute that single statement, but it has a lot of information in it and I want to use CURSORS to display it.IF you have any cursors code that will help me alot.
osilekpl at 2007-6-29 11:16:52 > top of Java-index,Archived Forums,Java Programming...
# 9

Presumably you want this to position in the set on a specific row...

Does postgres support stored procs? If so then you could do it that way.

Does the driver support some of the jdbc 2 stuff, then you could use that.

If not then you can write you own positional stuff simply by calling next() repeatedly without retrieving information.

jschell at 2007-6-29 11:16:52 > top of Java-index,Archived Forums,Java Programming...