存储过程 和存储函数

存储过程 和存储函数 - 图1

建存储过程

存储过程 和存储函数 - 图2

  1. create or replace procedure sayhelloword
  2. as
  3. --说明部分
  4. begin
  5. dbms_out_put_line("Hello world");
  6. end;
  7. /


调用存储过程

  1. exec sayhelloword();
  2. begin sayhelloword(); sayhelloword(); end;

存储过程 和存储函数 - 图3

创建存储函数

存储过程 和存储函数 - 图4

  1. create or replace function queryempincome(eno in number)
  2. return number
  3. as
  4. --定义变量保存员工的薪水和奖金
  5. psal emp.sal%type;
  6. pcome emp.comm%type;
  7. begin
  8. --得到该员工的月薪和奖金
  9. select sal,comm into psal,pcomm from emp where empno=eno;
  10. --直接返回年收入
  11. return psal*12 + nvl(pcomm,0);
  12. end;

java 中如何访问程序中的存储过程和存储函数

  1. // 数据库连接(JDBC)
  2. package com.claa.javabasic.Connect;
  3. import java.sql.*;
  4. /**
  5. * @Author: claa
  6. * @Date: 2021/06/27 09:43
  7. * @Description: Oracle Jdbc 的连接
  8. */
  9. public class JDBCUtilsO {
  10. private static String driver = "oracle.jdbc.OracleDriver";
  11. private static String url="jdbc:oracle:thin:@192.168.56.101:1521:orcl";
  12. private static String user ="scott";
  13. private static String password ="tiger";
  14. // 注册数据库的驱动
  15. static {
  16. try{
  17. Class.forName(driver);
  18. }catch (ClassNotFoundException e){
  19. throw new ExceptionInInitializerError(e);
  20. }
  21. }
  22. //获取数据库的驱动
  23. public static Connection getConnection(){
  24. try{
  25. return DriverManager.getConnection(url,user,password)
  26. }catch (SQLException e){
  27. e.printStackTrace();
  28. }
  29. return null;
  30. }
  31. // 释放数据库的资源
  32. public static void Release(Connection conn, Statement st, ResultSet rs) {
  33. try {
  34. if(rs!=null){
  35. rs.close();
  36. }
  37. } catch (SQLException e) {
  38. e.printStackTrace();
  39. }finally {
  40. drs = null;
  41. }
  42. try {
  43. if(st!=null){
  44. st.close();
  45. }
  46. } catch (SQLException e) {
  47. e.printStackTrace();
  48. }finally {
  49. st = null;
  50. }
  51. try {
  52. if(st!=null){
  53. st.close();
  54. }
  55. } catch (SQLException e) {
  56. e.printStackTrace();
  57. }finally {
  58. conn = null;
  59. }
  60. }
  61. }



// 调用存储过程和函数
 package com.claa.javabasic.Connect;

 import oracle.jdbc.OracleTypes;
 import org.junit.Test;

 import java.sql.CallableStatement;
 import java.sql.Connection;

 /**
  * @Author: claa
  * @Date: 2021/06/27 10:39
  * @Description:
  */
 public class TestProcedure {

     @Test
     public void testProcedure(){
         //存储过程 
         String sql = "{call queryempincome(?,?,?,?)} ";

         // 存储函数
         //String sql = "{?=call queryempincome(?,?,?,?)} ";

         Connection conn = null;

         CallableStatement call = null;
         try{
             // 得到一个连接
             conn = JDBCUtilsO.getConnection();

             // 通过连接创建出Statement
             call = conn.prepareCall(sql);

             // 对于 in参数
             call.setInt(1,7839);

             // 对于Out 参数
             call.registerOutParameter(2, OracleTypes.VARCHAR);
             call.registerOutParameter(3,OracleTypes.NUMBER);
             call.registerOutParameter(4,OracleTypes.VARCHAR);

             //执行调用
             call.execute();

             //取出结果
             String name = call.getString(2);
             System.out.println(name);

         }catch(Exception e){
            e.printStackTrace();
         }finally {
             JDBCUtilsO.Release(conn,call,null);
         }
     }
 }