ºÝºÝߣ

ºÝºÝߣShare a Scribd company logo
Cursor Demo App
package com.peeyush;
importjava.sql.CallableStatement;
importjava.sql.Connection;
importjava.sql.DriverManager;
importjava.sql.ResultSet;
importjava.util.Scanner;
importoracle.jdbc.*;
publicclassCursorDemo {
publicstaticvoidmain(String[] durga)throwsException{
//inputfromthe user
Scannersc=newScanner(System.in);
System.out.println("Entercharacters(firstlettersof empname):");
Stringinitchars=sc.next();
initchars=initchars+"%";
//registerthe driverandcreate connection
Class.forName("oracle.jdbc.driver.OracleDriver");
Connectioncon=
DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","system","password");
//create CallableStatementobject
CallableStatementcs=con.prepareCall("{call fetch_all_EmpDetails(?,?)}");
//registeroutparameterswithjdbctypes(Oraclecorpsuppliedjdbctypes)
cs.registerOutParameter(2,OracleTypes.CURSOR);
//setvalue toIN params
cs.setString(1,initchars);
//execute pl/sql procedure
cs.execute();
//gatherresultsfromoutparams
ResultSetrs=(ResultSet)cs.getObject(2);
//displaythe result
while(rs.next()){
System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getInt(3)+"
"+rs.getInt(4));
}
//close jdbcobj connection
rs.close();
cs.close();
con.close();
}
}

More Related Content

Cursor Demo App

  • 1. Cursor Demo App package com.peeyush; importjava.sql.CallableStatement; importjava.sql.Connection; importjava.sql.DriverManager; importjava.sql.ResultSet; importjava.util.Scanner; importoracle.jdbc.*; publicclassCursorDemo { publicstaticvoidmain(String[] durga)throwsException{ //inputfromthe user Scannersc=newScanner(System.in); System.out.println("Entercharacters(firstlettersof empname):"); Stringinitchars=sc.next(); initchars=initchars+"%"; //registerthe driverandcreate connection Class.forName("oracle.jdbc.driver.OracleDriver"); Connectioncon= DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","system","password"); //create CallableStatementobject CallableStatementcs=con.prepareCall("{call fetch_all_EmpDetails(?,?)}"); //registeroutparameterswithjdbctypes(Oraclecorpsuppliedjdbctypes) cs.registerOutParameter(2,OracleTypes.CURSOR); //setvalue toIN params cs.setString(1,initchars);
  • 2. //execute pl/sql procedure cs.execute(); //gatherresultsfromoutparams ResultSetrs=(ResultSet)cs.getObject(2); //displaythe result while(rs.next()){ System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getInt(3)+" "+rs.getInt(4)); } //close jdbcobj connection rs.close(); cs.close(); con.close(); } }