sql - Return cursor results from anonymous block -
i have following select want change anonymous block (need use anonymous block doing within java , not have access created stored functionality) in order remove use of literal bind variable:
select il.lat_num, il.lngtd_num item_location il il.item_id = 294341;
i have 2 anonymous blocks created unable find how return values created in both:
1)
declare itemid number; latitude number; longitude number; begin itemid := 294341; select il.lat_num, il.lngtd_num, latitude, longitude, item_location il il.item_id = itemid ; end;
2)
declare type t_ref_cursor ref cursor; c_cursor t_ref_cursor; itemid number; latitude item_location.lat_num%type; longitude item_location.lngtd_num%type; begin itemid := 294341; open c_cursor select il.lat_num, il.lngtd_num, item_location il il.item_id = itemid ; close c_cursor; end;
does know how either/both of these blocks can return if select above?
i want change anonymous block ... in order remove use of literal bind variable
why think need use anonymous block use bind variable? , both of blocks still have value 294341
hard-coded anyway; select
using bind variable within block generating new block every time of improvement on had?
as @haki said many hours ago, cam use prepared statement bind variable:
preparedstatement pstmt = conn.preparestatement( "select il.lat_num, il.lngtd_num : + "from item_location il " + "where il.item_id = ?"); pstmt.setint(1, 294341);
... , execute query , process result set presumably already. (from reply haki's comment seem confusing prepared statement - java/jdbc construct - stored procedure in database).
here's simple standalone example against demo hr
schema's emp
table:
import java.sql.; import java.text.; import oracle.jdbc.*; import oracle.jdbc.pool.oracledatasource;
public class jamesgallagher { public static void main(string args[]) throws sqlexception { connection conn; oracledatasource ds = new oracledatasource(); ds.seturl("jdbc:oracle:thin:scott/oracle@127.0.0.1:1521:orcl"); conn = ds.getconnection(); preparedstatement pstmt = conn.preparestatement( "select ename, sal emp sal > ?"); pstmt.setint(1, integer.parseint(args[0])); resultset rs = pstmt.executequery(); while (rs.next()) { system.out.println(rs.getstring(1) + ": " + rs.getint(2)); } try { rs.close(); } catch ( exception ex ) {} try { pstmt.close(); } catch ( exception ex ) {} try { conn.close(); } catch ( exception ex ) {} conn = null; } }
i can compile javac jamesgallagher.java
, execute java jamesgallagher 1500
, prints results based on bound value:
allen: 1600 jones: 2975 blake: 2850 clark: 2450 scott: 3000 king: 5000 ford: 3000
Comments
Post a Comment