关闭

JAVA调用存储过程

347人阅读 评论(0) 收藏 举报

下面是在网上找的一些关于用JAVA调用Oracle的函数,觉得不错,自己就整理了一下

create table TESTTB
(
ID VARCHAR2(30),
NAME VARCHAR2(30)
)

Insert into testtb values('1','21');
Insert into testtb values('2','22');
Insert into testtb values('3','23');
Insert into testtb values('4','24');

 

1、用来插入数据,没有返回值

CREATE OR REPLACE PROCEDURE TESTA(PARA1 IN VARCHAR2, PARA2 IN VARCHAR2)
AS
BEGIN
INSERT INTO HUANGBIAO.TESTTB(ID, NAME) VALUES (PARA1, PARA2);
END TESTA;

 

package hb.com;

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 oracle.jdbc.OracleTypes;

public class TestProcedureDemo1 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Connection conn = null;
		Statement stmt = null;
		CallableStatement proc = null;
		ResultSet rs = null;
		try {
			DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
			System.out.println("driver is ok");
			conn = DriverManager.getConnection(
					"jdbc:oracle:thin:@localhost:1521:orcl", "huangbiao", "huangbiao");
			proc = conn.prepareCall("{ call HUANGBIAO.TESTA(?,?) }");
			proc.setString(1, "100");
			proc.setString(2, "TestOne");
			proc.execute();
			
		} catch (SQLException e) {
			try {
				// 如果出现异常将操作回滚,这样就能保证数据的一致性
				conn.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			e.printStackTrace();
		}

		if (stmt != null) {
			try {
				stmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if (conn != null) {
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

}

 

2、查询只有一个返回值的结果集合

CREATE OR REPLACE PROCEDURE TESTB(PARA1 IN VARCHAR2, PARA2 OUT VARCHAR2)
AS
BEGIN
SELECT NAME INTO PARA2 FROM TESTTB WHERE ID = PARA1;
END TESTB;

 

package hb.com;

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;

import oracle.jdbc.OracleTypes;

public class TestProcedureDemo2 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Connection conn = null;
		Statement stmt = null;
		CallableStatement proc = null;
		ResultSet rs = null;
		try {
			DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
			System.out.println("driver is ok");
			conn = DriverManager.getConnection(
					"jdbc:oracle:thin:@localhost:1521:orcl", "huangbiao", "huangbiao");
			proc = conn.prepareCall("{ call HUANGBIAO.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 e) {
			try {
				// 如果出现异常将操作回滚,这样就能保证数据的一致性
				conn.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			e.printStackTrace();
		}

		if (stmt != null) {
			try {
				stmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if (conn != null) {
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

}

 

3、查询有多个返回值的集合(一组数据)

CREATE OR REPLACE PACKAGE TESTPACKAGE AS
TYPE TEST_CURSOR IS REF CURSOR;
end TESTPACKAGE;

CREATE OR REPLACE PROCEDURE TESTC(P_CURSOR out
TESTPACKAGE.TEST_CURSOR) IS
BEGIN
OPEN P_CURSOR FOR
SELECT * FROM HUANGBIAO.TESTTB;
END TESTC;

 

package hb.com;

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 oracle.jdbc.OracleTypes;

public class TestProcedureDemo3 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Connection conn = null;
		Statement stmt = null;
		CallableStatement proc = null;
		ResultSet rs = null;
		try {
			DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
			System.out.println("driver is ok");
			conn = DriverManager.getConnection(
					"jdbc:oracle:thin:@localhost:1521:orcl", "huangbiao", "huangbiao");
			proc = conn.prepareCall("{ call huangbiao.testc(?) }");
			proc.registerOutParameter(1,OracleTypes.CURSOR);
			proc.execute();
			rs = (ResultSet) proc.getObject(1);
			
			while (rs.next()) {
				System.out.println("<tr><td>" + rs.getString(1) + "</td><td>"
						+ rs.getString(2) + "</td></tr>");
			}
			
		} catch (SQLException e) {
			try {
				// 如果出现异常将操作回滚,这样就能保证数据的一致性
				conn.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			e.printStackTrace();
		}

		if (stmt != null) {
			try {
				stmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if (conn != null) {
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

}

  

附件中有我参考的文档,还有自己在本机上调试的代码

 

使用SQLPLUS调用存储过程

create or replace procedure proc_select(
table_id in varchar2
)as
hbsql varchar2(500);
begin
	hbsql:='select name from testtb where id='||table_id;
	execute immediate hbsql;
end;
备注:上面的变量不能使用sql,即“hbsql”不能写为“sql”
execute proc_select('3');


create or replace procedure proc_insert
(
id in varchar2, --输入序号
name in varchar2 --输入姓名
) as
str_sql varchar2(500);
begin
str_sql:='insert into testtb values(:1,:2)';
execute immediate str_sql using id,name; --动态执行插入操作
exception
when others then
null;
end ;

SQL> execute proc_insert('11',’dinya’);

 

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:312421次
    • 积分:8738
    • 等级:
    • 排名:第2257名
    • 原创:1096篇
    • 转载:14篇
    • 译文:0篇
    • 评论:5条
    最新评论