Logo 
Search:

Java Answers

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds
  Question Asked By: Jesse Black   on Jun 03 In Java Category.

  
Question Answered By: Nagaraju Iyaner   on Jun 03

Below is a small example of a java  Program calling oracle  Stored Proc.
May be this can help you

import java.util.Properties;
import java.sql.Driver;
import java.sql.Connection;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.lang.Integer;
import oracle.jdbc.OracleTypes;

public class CallProc
{
public static void main(String[] args)
{
Properties props = new java.util.Properties();
props.put("user", "SCOTT");
props.put("password", "TIGER");
try
{
Driver d = (Driver)Class.forName
("oracle.jdbc.driver.OracleDriver").newInstance();

Connection conn = d.connect
("jdbc:oracle:thin:@localhost:1521:efg8ir3", props);

String sProc = "begin ? := MySquare(?); end;";

CallableStatement stmt = conn.prepareCall
(sProc);
// register the type of the out param - an
Oracle specific type
stmt.registerOutParameter(1,
OracleTypes.INTEGER);

// set the in param
stmt.setInt(2, 5);// Float(2, price);

// execute and retrieve the result set
stmt.execute();
//Integer im = (Integer)stmt.getObject(1);
//System.out.println(im.intValue());
System.out.println(stmt.getInt(1));

stmt.close();
conn.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}

The code for oracle stored proc is
CREATE OR REPLACE FUNCTION MySquare(i number)
RETURN number
AS
BEGIN
RETURN i * i;
END;

Share: 

 
 
Didn't find what you were looking for? Find more on ORACLE Error ! Or get search suggestion and latest updates.


Tagged: