技术开发 频道

ORACLEERP开发基础之Oracle数据库基础

  PL/SQL过程调用表示

  PL/SQL过程调用支持两种参数表示方式,一种是位置表示法,如C、JAVA都是使用这种方式。第二种是名称表示法,这个恐怕只有PL/SQL使用,当然PL/SQL也是从Ada那边学来的。 名称表示法示例(注:仅限于过程,名称表示法不适用于函数。切记!):

  创建存储过程:

  create or replace procedure test_proc(i_temp number,j_temp number,c_temp varchar)

  is

  t varchar(30);

  begin t := i_temp || '' || j_temp || c_temp;

  dbms_output.put_line(t);

  end;

  调用:

  begin test_proc(j_temp=>10,i_temp => 60,c_temp => 'test');

  end;

  PL/SQL调用Java类

  作为JAVA程序员,对ORACLE的javasource一直是想找个机会下手的。呵呵!风高月夜,此时正下手之机。Oracle有提供一个叫“外部例程”来实现与其他编程语言的访问,但ORACLE特别集成了JAVA。例如:可以将业务逻辑写在JAVA类中,通PL SQL进行调用,从而实现了业务逻辑层与数据库分离。

  1.HelloWorld例子

  1.0创建java source

  create or replace and compile java source named JTest as

  public class JTest

  {

  public static String entry(String s,int i)

  {

  return "First "+i+" hello"+s;

  }

  }

  1.1创建调用function

  create or replace function read_string (t in varchar2,i number)

  return varchar2 is language java name ' JTest.entry(java.lang.String,int) return t';

  --注:function的形参应与调用的JAVA类中方法的形参相同

  1.2调用测试

  SELECT read_string(' world',10) from dual

  注意:在ORACLE中,JAVA SOURCE可以拥有非static方法,但PLSQ/包调用的方法必须是static的。

  2.再来一个实用一点的例子(JDBC) 2.1创建JAVA类

  create or replace and compile java source named testjdbc as

  import java.io.IOException; import java.sql.Connection;

  import java.sql.DriverManager; import java.sql.PreparedStatement; i

  mport java.sql.ResultSet; import java.sql.SQLException;

  /**

  * @author jarwang

  * @since 2008-6-12

  * @version xx1.0

  */

  public class TestJDBC{

  public static String queryTable(){ String temp="";

  Connection conn = null;

  PreparedStatement pstmt = null;

  ResultSet rs = null; try{ conn = getConn();

  pstmt = conn.prepareStatement("select hname from test ");

  rs = pstmt.executeQuery();

  while(rs.next()){ temp += rs.getString("hname");

  } }catch(Exception ioex){ System.out.println("failure: "+ioex);

  }finally{ closeConn(conn,pstmt,rs);

  } return temp;

  } public static void insertTable(String s){ Connection conn = null;

  PreparedStatement pstmt = null;

  ResultSet rs = null;

  String sql = "insert into test (hname)values('"+s+"')";

  try{ conn = getConn(); pstmt = conn.prepareStatement(sql);

  pstmt.execute();

  conn.commit();

  }catch(Exception oex){ System.out.println(" has failure: "+oex);

  try{

  conn.rollback();

  }catch(Exception ex){ ex.printStackTrace();

  }

  }finally{ try{ pstmt.close(); conn.close();

  }catch(Exception ex){ ex.printStackTrace(); }

  }

  } public static Connection getConn(){ Connection conn = null;

  String user = "";

  String pwd = "";

  String url = "jdbc:oracle:thin:@ip:sid"; t

  ry{ Class.forName("oracle.jdbc.driver.OracleDriver");

  conn = DriverManager.getConnection(url,user,pwd);

  conn.setAutoCommit(false);

  System.out.println(" OPEN DATABASE SUCCESS ");

  }catch(SQLException ex){ System.out.println(" OPEN DATABASE FAILURE: "+ex);

  }catch(Exception e){ System.out.println("JDBC CLASS LOADE FAILURE: "+e);

  } return conn;

  } public static void closeConn(Connection conn,PreparedStatement pstmt,ResultSet rs){ try{ rs.close();

  pstmt.close();

  conn.close();

  }catch(Exception e){ e.printStackTrace();

  }

  }

  }

  create or replace package TestJDBC is -- Author

  2.2创建PL/SQL包

  create or replace package TestJDBC is

  -- Author : JARWANG

  -- Created : 2008-6-12 10:24:51

  -- Purpose : TestJDBC

  procedure insert_table(t varchar2);

  function query_table return varchar2;

  end TestJDBC;

  create or replace package body TestJDBC is

  procedure insert_table(t varchar2) is

  language java name 'TestJDBC.insertTable(java.lang.String)';

  function query_table return varchar2 is language java name 'TestJDBC.queryTable() return String';

  end TestJDBC;

  2.3测试调用

  declare t varchar2(3000);

  begin TestJDBC.insert_table('JDBC例子');

  t := TestJDBC.query_table();

  dbms_output.put_line('t='||t);

  declare t varchar2(3000);

  end;

  3.调用含有外部的jar的JAVA类

  3.1在ORACLE中加载JAR包。

  3.1.1上传JAVA类

  上传jar包至ORACLE服务器。例:/data/test/test.jar

  3.1.2注册JAVA类

  CALL DBMS_JAVA.loadjava('/data/test/test.jar');

  3.1.3查询是否注册成功

  Select

  OBJECT_NAME, OBJECT_TYPE, STATUS, SYS.DBMS_JAVA.LONGNAME(OBJECT_NAME) LONGNAME, LAST_DDL_TIME, OBJECT_ID, uo.created

  from sys.user_objects uo

  where object_type in

  ('JAVA CLASS', 'JAVA SOURCE', 'JAVA RESOURCE', 'JAVA DATA')

  order by uo.created desc

0
相关文章