使用Java调用Oracle 存储过程实验

//1、编写一个测试表Book

create table book
(
id number primary key,
name varchar2(100),
description varchar2(100)
)

//2、定义一个插入数据的存储过程:
create or replace procedure addBook
(bookId number,bookName varchar2,description varchar2) is
begin
  insert into book values(bookId,bookName,description);
end;

//3、定义一个读取全部书籍和他们的数量的存储过程:

	//1)先定义一个游标
	create or replace package myPackage as
       type my_cursor is ref cursor;
end myPackage;

	//2)再定义一个过程返回游标给java
create or replace procedure getAll
(total out number,myCursor out myPackage.my_cursor) is
begin
   open myCursor for select * from book order by id desc;
   select count(*) into total from book;
end;

//3)在pl/sql里测试这个过程:
declare
  v_total number;
  v_cursor myPackage.my_cursor;
  v_bookRow book%rowtype;
begin
  getAll(v_total,v_cursor);
  dbms_output.put_line('total='||v_total);
  loop
      fetch v_cursor into v_bookRow.id,v_bookRow.name,v_bookRow.description;
      dbms_output.put_line('id='||v_bookRow.id||' name='||v_bookRow.name||' description='||v_bookRow.description);
       exit when v_cursor%notfound;
   end loop;
   close v_cursor;
end;
//测试成功

//4、编写普通的JDBC类对插入过程进行测试:
public static void main(String[] args) throws Exception{
		Class.forName("oracle.jdbc.driver.OracleDriver");
		Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@//192.168.0.150:1521/orcl","scott","tiger");
		CallableStatement cs = conn.prepareCall("{call addBook(?,?,?)}");
		cs.setInt(1,7);
		cs.setString(2,"Money in action");
		cs.setString(3,"description Money in action");
		cs.execute();
	}
//测试成功

//5、测试JDBC对查询过程的操作:
public static void main(String[] args) throws Exception{
		Class.forName("oracle.jdbc.driver.OracleDriver");
		Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@//192.168.0.150:1521/orcl","scott","tiger");
		
		CallableStatement cs = conn.prepareCall("{call getAll(?,?)}");
		cs.registerOutParameter(1,oracle.jdbc.OracleTypes.NUMBER);
		cs.registerOutParameter(2,oracle.jdbc.OracleTypes.CURSOR);
		cs.execute();
		//输出
		System.out.println("total="+cs.getDouble(1));
		
		ResultSet rs = (ResultSet)cs.getObject(2);
		while(rs.next()){
			System.out.println("id="+rs.getString("id")+" name="+rs.getString("name"));
		}
		
	}

//6、使用hibernate+spring调用存储过程:
	可以使用HibernateTemplate的回调函数来调用jdbc的方法:
getHibernateTemplate().execute(new HibernateCallback() { 
                public Object doInHibernate(Session session) { 
                    try { 
                        Connection conn = session.connection(); 

                        String sql = "{call readcountplusone(?)}"; 
                        CallableStatement stmt = conn.prepareCall(sql); 
                        stmt.setLong(1, pojo.getId().longValue()); 
                        stmt.execute(); 
                    } catch (Exception e) { 

                                e.printStackTrace(); 
                        } 
                    } 

                    return null; 
                } 
            }); 
//使用spring提供的getHibernateTemplate调用 

你可能感兴趣的:(java,spring,oracle,sql,jdbc)