Retieving an Oracle cursor in Java
Jan/100
Many people come here looking for one way to retrieve cursors of Oracle procedures in Java. To them, I’ve here are a tutorial showing how to do this.
To retrieve the cursor you should declare him how a REF CURSOR in Package spec.
--Creating the REF CURSOR type type g_cursor is ref cursor;
In both, spec and body, you need declare an out REF CURSOR variable in procedure signature, how cited above.
procedure PRO_RETURN_CARS(
i_id in tbl_car.car_id%type,
o_cursor in out g_cursor);
The cursor must be opened in procedure’s body to return, this way:
open o_cursor for
select car_id, company, model, color, hp, price
from tbl_car
where car_id = i_id;
The complete Package:
create or replace package PAC_CURSOR is
--Creating REF CURSOR type
type g_cursor is ref cursor;
--Procedure that return the cursor
procedure PRO_RETURN_CARS(
i_id in tbl_car.car_id%type,
o_cursor in out g_cursor); -- Our cursor
end PAC_CURSOR;
/
create or replace package body PAC_CURSOR is
procedure PRO_RETURN_CARS(
i_id in tbl_car.car_id%type,
o_cursor in out g_cursor) is
begin
--Opening the cursor to return matched rows
open o_cursor for
select car_id, company, model, color, hp, price
from tbl_car
where car_id = i_id;
end PRO_RETURN_CARS;
end PAC_CURSOR;
We have Oracle side ready, now we need create Java call
How the cursors are being returned by a procedure, we’ll used a java.sql.CallableStatement instance.
CallableStatement cs = conn.prepareCall("{call PAC_CURSOR.PRO_RETURN_CARS(?,?)}");
The registerOutParameter will obtain oracle.jdbc.OracleTypes.CURSOR type and return a java.sql.ResultSet instance. We can iterate the ResultSet like a common Iterator.
Each row column returned by SELECT will be represented how a map, using correspondent getter. For example, we will call getString(<column name>) method when value of column is a varchar, getDate(<column name>) when is a date and etc.
The complete code will be like this:
//Calling Oracle procedure
CallableStatement cs = conn.prepareCall("{call PAC_CURSOR.PRO_RETURN_CARS(?,?)}");
//Defining type of return
cs.registerOutParameter("o_cursor", OracleTypes.CURSOR);
cs.setLong("i_id", id);
cs.execute();//Running the call
//Retrieving the cursor as ResultSet
ResultSet rs = (ResultSet)cs.getObject("o_cursor");
//Iterating the returned rows
while(rs.next()){
//Getting column values
System.out.println("ID: " + rs.getLong("car_id"));
System.out.println("Manufacturer: " + rs.getString("company"));
System.out.println("Model: " + rs.getString("model"));
System.out.println("Color: " + rs.getString("color"));
System.out.println("HP: " + rs.getString("hp"));
System.out.println("Price: " + rs.getFloat("price"));
}
In the end you will get any value returned in a SELECT clause.
See ya!
You liked it? Then, please, make a comment! Campaign: "Comment doesn't make your fingers falls".How to enable Telnet and TFTP Client on Windows 7
Dec/090
Like in Windows Vista, Windows 7 don’t enable Telnet and TFTP Clients in installation.
To enable them, open Control Panel > Programs and Features > click Turn Windows features on or off in left side > enable Client Telnet and Client TFTP then click in OK.
I not tested in Windows Vista yet, but the process can be same.
See you soon!
You liked it? Then, please, make a comment! Campaign: "Comment doesn't make your fingers falls".The most used Unix commands for Windows
Nov/090
Have you thought about run commands like grep, chown, tail e su in Windows SO and can change command dir by ls?
Looking in internet for an alternative Win32 to command tail, I found UnixUtils. A compilation for Windows of most used commands in Linux/Unix.
You can download the ZIP file in SourceForge clicking here.
Bye!
You liked it? Then, please, make a comment! Campaign: "Comment doesn't make your fingers falls".Generating ‘EXE’ to start your Java applications
Oct/090
Many developers need, or have needed, distribute their Java applications so that Windows users could start them naturally, not running java -jar <jar file> command or batch (.BAT) file.
I’ve been there too, then I found a easy and with many resources solution: JSmooth
This software allow you “transform” your JAR file in an executable (EXE), but, of course, will need the JVM already installed and running.
Here I only have mentioned the settings that I consider important, then let’s go!
Download JSmooth in http://sourceforge.net/projects/jsmooth/files/;
After install (or unzip, this depends of file that you have downloaded) run him;
In the side menu, click in “Skeleton”;
In the “Skeleton Selection” screen you need define how application will be run, select “Window Wrapper”.
In “Skeleton Properties”, you need define a message when user have no JVM installed (“Message” field) and where can be downloaded (“URL” field).
The “Launch java app in the exe process” field define if JAR file will be executed in same process of EXE (only the executable process will be displayed in Windows Task Manager), otherwise the javaw.exe will be displayed too.
The “Single Instance” field define if more than one instance can be opened.
“Debug Console” open the EXE from prompt window, displaying possibles outputs.
Now click in “Executable”
In “Executable Setting” you inform where EXE will be builded (“Executable Binary” field), the EXE’s icon (“Executable Icon” filed) and what will be the application execution directory.
Click in “Application”
First, click in the icon
and select JAR that contains the main class.
After, select the main class in the field “Main Class” clicking in button
.
In the field “Application arguments” you can inform necessaries parameters for your main class.
“Embedded JAR” field allows you to aggregate your JAR file in EXE file, in other words, only EXE file will be necessarie, because the JAR will be uncompressed by EXE in each execution.
Now click in “JVM Selection”.
Here you can define the minimum and maximum version of JVM that your application support.
The “JVM Serach Sequence” inform the seek order of javaw.exe file, in this case, he search in resgistry first, after in JAVA_HOME enviroment and so.
Click in “JVM Configuration”.
Here the maximum and minimum memory available for your application can be configured, as the options that can passed to JVM.
Until here we only have configured the JSmooth. To build the EXE file click in button
. If you don’t have saved the project, a new window will open to choose the place to save. Done it, the EXE file will be create in directory mentioned in “Executable Binary” field of “Executable” screen.
Now just execute the EXE file and your application will be run!
To more information visit http://jsmooth.sourceforge.net/
I hope you enjoyed, feel free to comment!
Until next!
Retrieving objects collection from Oracle procedure
Sep/091
As I’ve promised in post “Learn to pass a Java Object as Oracle Procedure parameter“, I’ll show how retrieve object that have a collection of objects as attribute through of an Oracle procedure. Is highly recommended to read previous post.
For this tutorial, we’ll need create the table TBL_CLASS and add your primary key as foreign key in TBL_USER table.
--num class is PK and desc_class description create table TBL_CLASS (num_class number, desc_class varchar(100)); alter table TBL_CLASS add primary key(num_class); alter table TBL_USER add num_class number; alter table TBL_USER add constraint FK_CLASS foreign key(num_class) references tbl_class(num_class);
Now we need to include the new types:
create or replace type class_type as object (num_class number, desc_class varchar2(100), users arr_users); / create or replace type arr_class as table of class_type; /
The class_type type will be the Java Object. Notice that in your signature was included the arr_users type, that will be our collection of user_type (read previous post for more information), the arr_class type will be the class_type collection.
Now we’ll include the procedure that returns our class_type collection.
procedure pro_select_class(clas in class_type, class_return in out arr_class)is
class_ref_cur ref_cur;
--class_type array
classes arr_class := arr_class();
begin
open class_ref_cur for
select cast(
multiset(
select num_class,
desc_class,
(select cast(
multiset(
select user_name,
height,
b_date
from tbl_user
--JOIN with TBL_USER
where tbl_user.num_class = tbl_class.num_class
) as arr_users)
from dual) users
from tbl_class
--Using num_class attribute of in parameter
where num_class = clas.num_class) as arr_class
) classes
from dual;
--including the return in array
fetch class_ref_cur into classes;
--transferring arrar to variable out
class_return := classes;
end pro_select_class;
Notice that procedure receive class_type as parameter in and returns arr_class type.
Separating code charge back and set up our objects, we have:
--Mount return
select cast(
multiset(
--Will returns the objects class_type and your attributes
select num_class,
desc_class,
--Populate user_type collection
(select cast(
multiset(
select user_name,
height,
b_date
from tbl_user
where tbl_user.num_class = tbl_class.num_class
) as arr_users)
from dual) users
from tbl_class
where num_class = clas.num_class) as arr_class
) classes
from dual;
Oracle objects done, now the Java code!
We’ll create the object that will be interpreted by the Oracle. Called TypeClass:
public class TypeClass implements SQLData{
public static final String ORACLE_OBJECT_NAME = "CLASS_TYPE"; //Type name in Oracle
public static final String ORACLE_CLASS_ARRAY_NAME = "ARR_CLASS"; //Array name in Oracle
//Attibutes of TBL_CLASS table
private Long number;
private String desc;
private Array users; //This will be user_type collection (or TypeUser in Java)
public String getSQLTypeName() throws SQLException {
return ORACLE_OBJECT_NAME;
}
public void readSQL(SQLInput stream, String typeName) throws SQLException {
setNumber(stream.readLong());
setDesc(stream.readString());
setUsers(stream.readArray());//Used by JDBC driver to read the collection
}
public void writeSQL(SQLOutput stream) throws SQLException {
stream.writeLong(getNumber());
stream.writeString(getDesc());
stream.writeArray(getUsers());//Used by JDBC driver to write the collection
}
//Getters and setters omitted
}
We need to map types interpreted in request, this way:
Map> typeMaps = connection.getTypeMap(); typeMaps.put(TypeUser.ORACLE_OBJECT_NAME, TypeUser.class); typeMaps.put(TypeClass.ORACLE_OBJECT_NAME, TypeClass.class);
We need to map the arrays too:
typeMaps.put(TypeClass.ORACLE_CLASS_ARRAY_NAME, TypeClass[].class);//returned by procedure typeMaps.put(TypeUser.ORACLE_USER_ARRAY_NAME, TypeUser[].class);//returned by class_type collection
For request, we do:
cs = conn.prepareCall("{call PAC_BEAN.PRO_SELECT_CLASS(?,?)}");
//registering out type, that will be a TypeClass array
cs.registerOutParameter("class_return", OracleTypes.ARRAY, TypeClass.ORACLE_CLASS_ARRAY_NAME);
//passing parameter object
cs.setObject("clas", classQry);
cs.execute();
//retrieving and looping the TypeClass array
Object[] array = (Object[])cs.getArray("class_return").getArray();
for(Object obj : array){
TypeClass objClass = ((TypeClass)obj);
System.out.println("Description: "+objClass.getDesc());
//Here we obtains user_type(TypeUser) array returned by query.
Object[] userArray = (Object[])objClass.getUsers().getArray();
for(Object user : userArray){
System.out.println("\tName: " + ((TypeUser)user).getName());
System.out.println("\tHeight: " + ((TypeUser)user).getHeight());
System.out.println("\tBirth: " + sdf.format(((TypeUser)user).getBirth())+ "\r\n");
}
}
In the end you’ll have a java.sql.Array of TypeUser in getUsers() attribute of TypeClass.
Here I fulfilled my promise. Download the source code of this sample (with previous post sample too) here.
Until next time!
You liked it? Then, please, make a comment! Campaign: "Comment doesn't make your fingers falls".
Português



