服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - java调用Oracle存储过程的方法实例

java调用Oracle存储过程的方法实例

2019-10-11 11:47java教程网 Java教程

这篇文章介绍了java调用Oracle存储过程的方法实例,有需要的朋友可以参考一下

1.测试添加数据的procedure

 

复制代码代码如下:


public void testProcedure() {
        Connection con = getConnction();

 

        // **1.测试添加数据的procedure
          String procedure = "{call users_insert_proc(?,?,?,?) }";

        CallableStatement cs = null;
        try {
             cs = con.prepareCall(procedure);
             cs.setInt(1, 123450);
             cs.setString(2, "xxiaox");
             cs.setString(3, "Ww342864");
             cs.setString(4, "742621646@qq.com");
             } catch (SQLException e) {
              e.printStackTrace();
        }
        try {
             cs.executeUpdate();
        } catch (SQLException e) {
               e.printStackTrace();
        }
    }


2.测试删除数据的procedure

 

 

复制代码代码如下:


public void testDelPro() {
        Connection con = getConnction();

 

        // **2.测试删除数据的procedure
         String procedure = "{call delete_usersbyid_proc(?) }";

        CallableStatement cs = null;
        try {
             cs = con.prepareCall(procedure);
                      cs.setInt(1, 123450);
             } catch (SQLException e) {
              e.printStackTrace();
        }
        try {
             cs.executeUpdate();
        } catch (SQLException e) {
               e.printStackTrace();
        }
    }


3.测试更新数据的procedure

 

 

复制代码代码如下:


public void testDelPro() {
        Connection con = getConnction();

 

        // **3.测试更新数据的procedure
        String procedure = "{call users_updatebyId_proc(?,?,?,?) }";

        CallableStatement cs = null;
        try {
             cs = con.prepareCall(procedure);
         cs.setInt(1, 101);          cs.setString(2, "小第三方的浩");          cs.setString(3, "asdf342864");         cs.setString(4, "742621646@qq.com");
             } catch (SQLException e) {
              e.printStackTrace();
        }
        try {
             cs.executeUpdate();
        } catch (SQLException e) {
               e.printStackTrace();
        }
    }

 

4.测试查找数据的procedure

      a)建包体

      b)创建查询的procedure

 

复制代码代码如下:

create or replace package userspackage as
type users_cursor is ref cursor;
end  userspackage;

 

复制代码代码如下:


create or replace procedure users_packageAll(
s_id in number ,u_cursor out userspackage.users_cursor) is
begin
   if s_id = 0 then
       open u_cursor for select id,name,pword,email  from users;
      else
       open u_cursor for select id,name,pword,email  from users where id=s_id;
      end if;

 

  end;

 

c)Java调用

 

复制代码代码如下:


public void testDelPro() {
        Connection con = getConnction();

 

        // 返回查询procedure
       String procedure = "{call users_packageAll(?,?) }";


        CallableStatement cs = null;
        try {
             cs = con.prepareCall(procedure);
                     cs.setInt(1, 0);
     cs.registerOutParameter(2, oracle.jdbc.OracleTypes.CURSOR);

             } catch (SQLException e) {
              e.printStackTrace();
        }
        try {
             cs.execute();
              ResultSet rs = (ResultSet)cs.getObject(2);
            while (rs.next()) {
              System.out.println(rs.getInt(1) + " " + rs.getString(2));
            }
        } catch (SQLException e) {
               e.printStackTrace();
        }
    }

 

延伸 · 阅读

精彩推荐