• 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);

    }

  • 相关阅读:
    宝塔相关问题
    免费xshell下载
    服务器断电mysql无法恢复
    mysql相关知识
    svn
    tortoisesvn下载 和svn 安装
    nginx+lua乐观锁实现秒杀
    c# asp.net 生成唯一订单号
    c# 关闭软件 进程 杀死进程
    国内开源软件镜像地址搜集
  • 原文地址:https://www.cnblogs.com/angangxiaofa/p/7193931.html
Copyright © 2020-2023  润新知