使用JDBC调用Oracle的存储过程详解

1.Oracle的存储过程

--练习:使用out参数类型实例返回指定员工的姓名和工资
create or replace procedure findinfo (v_classid in varchar2,v_name out varchar2,v_salary out varchar2)
as
       
begin
       select classname,salary into v_name,v_salary from classinfo where classid=v_classid;
end;
/

 

2.JDBC调用Oracle的存储过程的Java代码

package cn.itcast.procedure;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;

public class Main {
	//使用JDBC调用存储过程
	public static void main(String[] args) throws Exception{
		
		/**
		 * mysql:
		 * 	com.mysql.jdbc.Driver
		 * 	jdbc:mysql//localhost
		 * 	root
		 * 	root
		 * 
		 * Oracle
		 * 	oracle.jdbc.OracleDriver
		 * 	jdbc:oracle:thin:@localhost:1521:orcl
		 * 	SCOTT
		 * 	Root123*/
		
		//1.定义变量
		String driverClass="oracle.jdbc.OracleDriver";
		String url="jdbc:oracle:thin:@localhost:1521:orcl";
		String user="SCOTT";
		String psw="Root123";
		
		//加载驱动
		Class.forName(driverClass);
		
		//2.获取连接对象
		Connection conn = DriverManager.getConnection(url,user,psw);
		
		//3.创建执行存储过程的语句对象
		String sql="{call SYS.FINDINFO(?,?,?)}";
		CallableStatement call = conn.prepareCall(sql);
		
		//4.设置参数类型
		call.setString(1, "002");
		call.registerOutParameter(2, oracle.jdbc.OracleTypes.VARCHAR);
		call.registerOutParameter(3, oracle.jdbc.OracleTypes.NUMBER);
		
		//5.执行
		call.execute();
		
		//6.获取数据
		String v_name=call.getString(2);
		Integer v_salary=call.getInt(3);
		System.out.println("员工名为:"+v_name+",工资是:"+v_salary);
		
		//7.释放资源
		call.close();
		conn.close();
		
	}

}

 

你可能感兴趣的:(Oracle,oracle,java,jdbc,数据库,mysql)