Oracle Cursor Query

Submitted by:Jhon Jhon

Date added:07 December, 2013

Category:Other

Example code for Oracle Cursor Query

Tags: cursor , query

Code Snippet:

    (Extracted from http://asktom.oracle.com/tkyte/ResultSets/)
import java.sql.*;
import java.io.*;
import oracle.jdbc.driver.*;

class curvar
{
public static void main (String args [])
throws SQLException, ClassNotFoundException
{
String driver_class = "oracle.jdbc.driver.OracleDriver";
String connect_string = "jdbc:oracle:thin:@slackdog:1521:oracle8";

String query = "begin :1 := sp_listEmp; end;";
Connection conn;

Class.forName(driver_class);
conn = DriverManager.getConnection(connect_string, "scott", "tiger");

CallableStatement cstmt = conn.prepareCall(query);
cstmt.registerOutParameter(1,OracleTypes.CURSOR);
cstmt.execute();
ResultSet rset = (ResultSet)cstmt.getObject(1);

while (rset.next ())
System.out.println( rset.getString (1) );
cstmt.close();
}
}
 
 

Comments