package com.founder.ec.common.lucene;import java.sql.CallableStatement;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import oracle.jdbc.internal.OracleCallableStatement;import oracle.jdbc.internal.OracleTypes;public class ReadMysql { public static Connection getConnection() throws Exception { String url = "jdbc:oracle:thin:@192.168."; Class.forName("oracle.jdbc.driver.OracleDriver"); String userName = ""; String password = ""; Connection con = DriverManager.getConnection(url, userName, password); return con; } /** * CREATE PROCEDURE member_procedure_out(memberId in NUMBER,realname OUT VARCHAR2,mobile_phone OUT VARCHAR2) *///释放数据库资源 public static void release(Connection con, CallableStatement call, ResultSet res) { if (res != null || call != null || con != null) { try { res.close(); con.close(); call.close(); } catch (Exception e) { e.printStackTrace(); } finally { res = null; call = null; res = null; } } } public static void main(String[] args) { /** * CREATE PACKAGE mypackage AS TYPE empcursor is REF CURSOR ; PROCEDURE queryEmpList(memberId in NUMBER,empList OUT empcursor); END mypackage; */ try { //调用存储过程,根据会员id获得会员name // testpro(); //调用存储函数,根据会员id计算其年收入 //testfunction(); //调用包下的存储过程,游标 testCursor(); } catch (Exception e) { e.printStackTrace(); } finally { // release(con, call, null); } } private static void testCursor() { Connection con = null; CallableStatement call = null; try { String sql = "{call mypackage.queryEmpList(?,?)}"; con = getConnection(); call = con.prepareCall(sql); call.setInt(1,7923); call.registerOutParameter(2,OracleTypes.CURSOR);//光标 call.execute(); //取出该会员的所有信息 ResultSet res = ((OracleCallableStatement) call).getCursor(2); while (res.next()){ String name=res.getString("real_name"); int memberId=res.getInt("member_id"); System.out.print(name+memberId); } } catch (Exception e) { e.getMessage(); } } private static void testfunction() { Connection con = null; CallableStatement call = null; try { String sql = "{?=call query_member(?)}"; con = getConnection(); call = con.prepareCall(sql); call.registerOutParameter(1,OracleTypes.NUMBER); call.setInt(2,7923); call.execute(); double sum=call.getDouble(1); System.out.println(sum); } catch (Exception e) { e.getMessage(); } } private static void testpro() { Connection con = null; CallableStatement call = null; try { String sql = "{call member_procedure_out(?,?)}"; con = getConnection(); call = con.prepareCall(sql); call.setInt(1, 7923); call.registerOutParameter(2, OracleTypes.VARCHAR); } catch (Exception e) { e.getMessage(); } try { call.execute(); String realName = call.getString(2); System.out.println(realName); } catch (Exception e) { e.printStackTrace(); } }}
create PROCEDURE raise_order_price_procedure(orderId in NUMBER ) as psal ORDER_INFO.paid_fee%TYPE ; BEGIN select paid_fee into psal from ORDER_INFO where ORDER_ID=orderId; update ORDER_INFO set paid_fee =paid_fee+100 where ORDER_ID=orderId; dbms_output.put_line('涨前:'||psal||'涨后:'||(psal+100)); END;drop PROCEDURE member_procedure_out; CREATE PROCEDURE member_procedure_out(memberId in NUMBER,realname OUT VARCHAR2) AS BEGIN select m.REAL_NAME into realname from member m where m.MEMBER_ID=memberId; END;/--BEGIN raise_order_price_procedure(102467);END; CREATE FUNCTION query_member(memberId in NUMBER) RETURN NUMBER AS psal member.account_fee%type; pcomm member.sum_points%type; BEGINselect account_fee , sum_points INTO psal,pcomm from MEMBER where MEMBER_ID=memberId; RETURN psal*12+nvl(pcomm,0); END; drop PACKAGE mypackage;CREATE PACKAGE mypackage AS TYPE empcursor is REF CURSOR ; PROCEDURE queryEmpList(memberId in NUMBER,empList OUT empcursor); END mypackage;create or replace PACKAGE BODY mypackage AS PROCEDURE queryEmpList(memberId in NUMBER,empList OUT empcursor)as BEGIN OPEN empList for select * from MEMBER where MEMBER_ID=memberId; END queryEmpList; END mypackage; create package emp_pkg is procedure emp_update_ename(v_empno varchar2,v_ename varchar2); function emp_get_sal(v_empno varchar2) return number; end;select * from member where MEMBER_ID=7923