浏览 6514 次
锁定老帖子 主题:java中调用ORACLE存储过程
精华帖 (0) :: 良好帖 (1) :: 新手帖 (1) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2010-11-22
存储过程为: CREATE OR REPLACE PROCEDURE TESTA(PARA1 IN VARCHAR2,PARA2 IN VARCHAR2) AS BEGIN INSERT INTO HYQ.B_ID (I_ID,I_NAME) VALUES (PARA1, PARA2); END TESTA; 然后呢,在java里调用时就用下面的代码: package com.hyq.src; import java.sql.*; import java.sql.ResultSet; public class TestProcedureOne { public TestProcedureOne() { } public static void main(String[] args ){ String driver = "oracle.jdbc.driver.OracleDriver"; String strUrl = "jdbc:oracle:thin:@127.0.0.1:1521: hyq "; Statement stmt = null; ResultSet rs = null; Connection conn = null; CallableStatement cstmt = null; try { Class.forName(driver); conn = DriverManager.getConnection(strUrl, " hyq ", " hyq "); CallableStatement proc = null; proc = conn.prepareCall("{ call HYQ.TESTA(?,?) }"); proc.setString(1, "100"); proc.setString(2, "TestOne"); proc.execute(); } catch (SQLException ex2) { ex2.printStackTrace(); } catch (Exception ex2) { ex2.printStackTrace(); } finally{ try { if(rs != null){ rs.close(); if(stmt!=null){ stmt.close(); } if(conn!=null){ conn.close(); } } } catch (SQLException ex1) { } } } } 当然了,这就先要求要建张表TESTTB,里面两个字段(I_ID,I_NAME)。 二:有返回值的存储过程(非列表) 存储过程为: CREATE OR REPLACE PROCEDURE TESTB(PARA1 IN VARCHAR2,PARA2 OUT VARCHAR2) AS BEGIN SELECT INTO PARA2 FROM TESTTB WHERE I_ID= PARA1; END TESTB; 在java里调用时就用下面的代码: package com.hyq.src; public class TestProcedureTWO { public TestProcedureTWO() { } public static void main(String[] args ){ String driver = "oracle.jdbc.driver.OracleDriver"; String strUrl = "jdbc:oracle:thin:@127.0.0.1:1521:hyq"; Statement stmt = null; ResultSet rs = null; Connection conn = null; try { Class.forName(driver); conn = DriverManager.getConnection(strUrl, " hyq ", " hyq "); CallableStatement proc = null; proc = conn.prepareCall("{ call HYQ.TESTB(?,?) }"); proc.setString(1, "100"); proc.registerOutParameter(2, Types.VARCHAR); proc.execute(); String testPrint = proc.getString(2); System.out.println("=testPrint=is="+testPrint); } catch (SQLException ex2) { ex2.printStackTrace(); } catch (Exception ex2) { ex2.printStackTrace(); } finally{ try { if(rs != null){ rs.close(); if(stmt!=null){ stmt.close(); } if(conn!=null){ conn.close(); } } } catch (SQLException ex1) { } } } } } 注意,这里的proc.getString(2)中的数值2并非任意的,而是和存储过程中的out列对应的,如果out是在第一个位置,那就是proc.getString(1),如果是第三个位置,就是proc.getString(3),当然也可以同时有多个返回值,那就是再多加几个out参数了。 三:返回列表 由于oracle存储过程没有返回值,它的所有返回值都是通过out参数来替代的,列表同样也不例外,但由于是集合,所以不能用一般的参数,必须要用pagkage了.所以要分两部分, 1, 建一个程序包。如下: CREATE OR REPLACE PACKAGE TESTPACKAGE AS TYPE Test_CURSOR IS REF CURSOR; end TESTPACKAGE; 2,建立存储过程,存储过程为: CREATE OR REPLACE PROCEDURE TESTC(p_CURSOR out TESTPACKAGE.Test_CURSOR) IS BEGIN OPEN p_CURSOR FOR SELECT * FROM HYQ.TESTTB; END TESTC; 可以看到,它是把游标(可以理解为一个指针),作为一个out 参数来返回值的。 在java里调用时就用下面的代码: package com.hyq.src; import java.sql.*; import java.io.OutputStream; import java.io.Writer; import java.sql.PreparedStatement; import java.sql.ResultSet; import oracle.jdbc.driver.*; public class TestProcedureTHREE { public TestProcedureTHREE() { } public static void main(String[] args ){ String driver = "oracle.jdbc.driver.OracleDriver"; String strUrl = "jdbc:oracle:thin:@127.0.0.1:1521:hyq"; Statement stmt = null; ResultSet rs = null; Connection conn = null; try { Class.forName(driver); conn = DriverManager.getConnection(strUrl, "hyq", "hyq"); CallableStatement proc = null; proc = conn.prepareCall("{ call hyq.testc(?) }"); proc.registerOutParameter(1,oracle.jdbc.OracleTypes.CURSOR); proc.execute(); rs = (ResultSet)proc.getObject(1); while(rs.next()) { System.out.println("" + rs.getString(1) + ""+rs.getString(2)+""); } } catch (SQLException ex2) { ex2.printStackTrace(); } catch (Exception ex2) { ex2.printStackTrace(); } finally{ try { if(rs != null){ rs.close(); if(stmt!=null){ stmt.close(); } if(conn!=null){ conn.close(); } } } catch (SQLException ex1) { } } } } 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2011-04-17
当然了,这就先要求要建张表TESTTB,里面两个字段(I_ID,I_NAME)。
二:有返回值的存储过程(非列表) 我问下,对于第二种方法,我建的表是TESTTB,两个字段分别定义为varchar2类型, 存储过程对吗?》下面这个 CREATE OR REPLACE PROCEDURE TESTB(PARA1 IN VARCHAR2,PARA2 OUT VARCHAR2) AS BEGIN SELECT INTO PARA2 FROM TESTTB WHERE I_ID= PARA1; END TESTB; 是不是正确的是这样子的, CREATE OR REPLACE PROCEDURE TESTB(PARA1 IN VARCHAR2,PARA2 OUT VARCHAR2) AS BEGIN SELECT I_NAME INTO PARA2 FROM TESTTB WHERE I_ID= PARA1; END TESTB; 我是刚刚入门的,不懂存储过程,只是在这里冒昧的问一下,手里拿板砖的不要拍,手下留情。 我昨天试了一下,好像不怎么管用啊? 不对。 我说下,我昨天的做的过程, 】首先oracle数据库建表 -- Create table create table TESTTB ( I_ID VARCHAR2(300), I_NAME VARCHAR2(300) ) tablespace USERS pctfree 10 initrans 1 maxtrans 255 storage ( initial 64 minextents 1 maxextents unlimited ); 存储过程,在plsql里面建立的时候,上面第二种方法的存储过程是直接提示就不能建,我自己的做法是: CREATE OR REPLACE PROCEDURE TESTB(PARA1 IN VARCHAR2,PARA2 OUT VARCHAR2) AS BEGIN SELECT I_NAME INTO PARA2 FROM TESTTB WHERE I_ID= PARA1; END TESTB; 然后java调用代码是: package com.hyq.src; import java.sql.CallableStatement; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.sql.Types; public class TestProcedureTWO { public static void main(String[] args ){ String driver = "oracle.jdbc.driver.OracleDriver"; String strUrl = "jdbc:oracle:thin:@localhost:1521:orcl"; Statement stmt = null; ResultSet rs = null; Connection conn = null; try { Class.forName(driver).newInstance(); conn = DriverManager.getConnection(strUrl, "zhang", "zhang"); CallableStatement proc = null; proc = conn.prepareCall("{call ZHANG.TESTB(?,?) }"); proc.setString(1, "100"); proc.registerOutParameter(2, Types.VARCHAR); proc.execute(); String testPrint = proc.getString(2); System.out.println("=testPrint=is="+testPrint); }catch (Exception e) { e.printStackTrace(); }finally{ try{ if(rs != null){ rs.close(); } if(stmt!=null){ stmt.close(); } if(conn!=null){ conn.close(); } }catch(Exception e){ e.printStackTrace(); } } } } 报的错误是: java.sql.SQLException: ORA-01403: no data found ORA-06512: at "ZHANG.TESTB", line 3 ORA-06512: at line 1 at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:125) at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:305) at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:272) at oracle.jdbc.driver.T4C8Oall.receive(T4C8Oall.java:623) at oracle.jdbc.driver.T4CCallableStatement.doOall8(T4CCallableStatement.java:180) at oracle.jdbc.driver.T4CCallableStatement.execute_for_rows(T4CCallableStatement.java:783) at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1028) at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:2888) at oracle.jdbc.driver.OraclePreparedStatement.execute(OraclePreparedStatement.java:2979) at oracle.jdbc.driver.OracleCallableStatement.execute(OracleCallableStatement.java:4103) at com.hyq.src.TestProcedureTWO.main(TestProcedureTWO.java:26) 请各位帮忙参考一下,谢谢 |
|
返回顶楼 | |
发表时间:2011-04-17
第一种,我试了一下是成功了。
可以执行插入语句, 但是第三种提示内存溢出, Exception in thread "main" java.lang.OutOfMemoryError: Java heap space at oracle.jdbc.driver.T4CMAREngine.unmarshalDALC(T4CMAREngine.java:2099) at oracle.jdbc.driver.T4C8TTIuds.unmarshal(T4C8TTIuds.java:132) at oracle.jdbc.driver.T4CTTIdcb.receiveCommon(T4CTTIdcb.java:126) at oracle.jdbc.driver.T4CTTIdcb.receiveFromRefCursor(T4CTTIdcb.java:104) at oracle.jdbc.driver.T4CResultSetAccessor.unmarshalOneRow(T4CResultSetAccessor.java:165) at oracle.jdbc.driver.T4CTTIrxd.unmarshal(T4CTTIrxd.java:788) at oracle.jdbc.driver.T4CTTIrxd.unmarshal(T4CTTIrxd.java:705) at oracle.jdbc.driver.T4C8Oall.receive(T4C8Oall.java:527) at oracle.jdbc.driver.T4CCallableStatement.doOall8(T4CCallableStatement.java:180) at oracle.jdbc.driver.T4CCallableStatement.execute_for_rows(T4CCallableStatement.java:783) at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1028) at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:2888) at oracle.jdbc.driver.OraclePreparedStatement.execute(OraclePreparedStatement.java:2979) at oracle.jdbc.driver.OracleCallableStatement.execute(OracleCallableStatement.java:4103) at com.hyq.src.TestProcedureTHREE.main(TestProcedureTHREE.java:28) 大家帮忙分析一下第三种的情况,是由于什么造成的? 不要拿板砖呼。我怕 |
|
返回顶楼 | |
发表时间:2011-09-01
我要去研究一下哒
|
|
返回顶楼 | |