• java调用数据库中的函数和存储过程


           1.调用函数

      {?= call <procedure-name>[(<arg1>,<arg2>, ...)]}

      {call <procedure-name>[(<arg1>,<arg2>, ...)]}*/

    public int testFunction() {

    String sql = "{?= call cal_add(?, ?)}";

    conn = DBHelper.getOracleConnection();

    int r = -1;

    try {

    cs = conn.prepareCall(sql);

    cs.registerOutParameter(1, OracleTypes.NUMBER);

    cs.setInt(2, 5);

    cs.setInt(3, 7);

    cs.execute();

    r = cs.getInt(1);

    } catch (SQLException e) {

    e.printStackTrace();

    }

    DBHelper.destroy(conn, cs, rs);

    return r;

    }

    2. 调用存储过程

    public void testProcedure() {

    String sql = "{call myprowithout(?,?,?)}";

    conn = DBHelper.getOracleConnection();

    try {

    cs = conn.prepareCall(sql);

    cs.setInt(1, 109);

    cs.registerOutParameter(2, OracleTypes.VARCHAR);

    cs.registerOutParameter(3, OracleTypes.VARCHAR);

    cs.execute();

    System.out.println(cs.getString(2));

    System.out.println(cs.getString(3));

    } catch (SQLException e) {

    e.printStackTrace();

    }

    DBHelper.destroy(conn, cs, rs);

    }

    3.调用带游标的存储过程

    public void testProcedureWithCursor() {

    String sql = "{call print_stu.put_stuinfo(?,?)}";

    conn = DBHelper.getOracleConnection();

    try {

    cs = conn.prepareCall(sql);

    cs.setInt(1, 95031);

    cs.registerOutParameter(2, OracleTypes.CURSOR);

    cs.execute();

    OracleCallableStatement ocs = (OracleCallableStatement)cs;

    rs = ocs.getCursor(2);

    while(rs.next()) {

    System.out.print(rs.getString("sno")+" ");

    System.out.print(rs.getString("sname")+" ");

    System.out.print(rs.getString("ssex")+" ");

    System.out.print(rs.getDate("sbirthday")+" ");

    System.out.println(rs.getInt("class"));

    }

    } catch (SQLException e) {

    e.printStackTrace();

    }

    DBHelper.destroy(conn, cs, rs);

    }

  • 相关阅读:
    springIOC 原理
    jeesite异步分页
    yum
    乐观锁原理
    equlas(),hashcode(),hashset,hashmap
    链接收藏
    java单词
    jeesite优化
    SailingEase .NET Resources Tool (.NET 多语言资源编辑器)转
    C#基本语法
  • 原文地址:https://www.cnblogs.com/angangxiaofa/p/7193931.html
Copyright © 2020-2023  润新知