Java invokes a method instance of an Oracle stored procedure

  • 2020-04-01 02:11:40
  • OfStack

  1. Test the procedure for adding data


public void testProcedure() {
        Connection con = getConnction();
        //**1. Test the procedure of adding data
          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. Test the procedure of data deletion


public void testDelPro() {
        Connection con = getConnction();
        //**2. Test the procedure of deleting data
         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. Test the procedure of updating data


public void testDelPro() {
        Connection con = getConnction();
        //**3. Procedure for testing updated data
        String procedure = "{call users_updatebyId_proc(?,?,?,?) }";
        CallableStatement cs = null;
        try {
             cs = con.prepareCall(procedure);
         cs.setInt(1, 101);          cs.setString(2, " Little third party hao ");          cs.setString(3, "asdf342864");         cs.setString(4, "742621646@qq.com");
             } catch (SQLException e) {
              e.printStackTrace();
        }
        try {
             cs.executeUpdate();
        } catch (SQLException e) {
               e.printStackTrace();
        }
    }

4. Test the procedure for finding data

          A) build inclusions

          B) procedure for creating a query


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 calls


public void testDelPro() {
        Connection con = getConnction();
        //Return query 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();
        }
    }


Related articles: