JDBC Tutorial

Callable Statement

FunConcept.java:

import java.sql.*;
import java.io.*;

class FunConcept {

    public static void main(String[] args) {
        try {
            DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
            System.out.println("DRIVERS LOADED...");
            Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:BudDinu", "scott", "tiger");
            System.out.println("CONNECTION OBTAINED...");
            DataInputStream dis = new DataInputStream(System.in);
            System.out.println("ENTER FIRST NUMBER : ");
            String s1 = dis.readLine();
            System.out.println("ENTER SECOND NUMBER : ");
            String s2 = dis.readLine();
            int n1 = Integer.parseInt(s1);
            int n2 = Integer.parseInt(s2);
            CallableStatement cs = con.prepareCall("{?=call ArthFun (?,?,?)}");
            cs.setInt(2, n1);
            cs.setInt(3, n2);
            cs.registerOutParameter(1, Types.INTEGER);
            cs.registerOutParameter(4, Types.INTEGER);
            cs.execute();
            int res = cs.getInt(1);
            int res1 = cs.getInt(4);
            System.out.println("SUM OF THE NUMBERS : " + res);
            System.out.println("MULTIPLICATION OF THE NUMBERS  : " + res1);
        } catch (Exception e) {
            e.printStackTrace();

        }
    }// main
}// FunConcept

Write a java program which illustrates the concept of procedure?

Answer:

StuPro:

create or replace procedure StuPro
(no in number, name in varchar2, loc1 out varchar2) 
as
begin
    select dname, loc into name, loc1 from dept
    where deptno=no;
    insert int abc values (no, name, loc1);
end;
/

ProConcept.java:

import java.sql.*;
import java.io.*;

class ProConcept {

    public static void main(String[] args) {
        try {
            DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
            System.out.println("DRIVERS LOADED...");
            Connection con = DriverManager.getConnection("dbc:oracle:thin:@localhost:1521:BudDinu", "scott", "tiger");
            DataInputStream dis = new DataInputStream(System.in);
            System.out.println("ENTER DEPARTMENT NUMBER : ");
            String s1 = dis.readLine();
            int n1 = Integer.parseInt(s1);
            CallableStatement cs = con.prepareCall("{call StuPro (?,?,?)}");
            cs.setInt(1, n1);
            cs.registerOutParameter(2, Types.VARCHAR);
            cs.registerOutParameter(3, Types.VARCHAR);
            cs.execute();
            String res = cs.getString(2);
            String res1 = cs.getString(3);
            System.out.println("DEPARTMENT NAME : " + res);
            System.out.println("DEPARTMENT LOCATION : " + res1);
        } catch (Exception e) {

            System.out.println(e);
        }
    }// main
}// ProConcept