1. 程式人生 > >Java程式碼呼叫儲存過程和儲存方法

Java程式碼呼叫儲存過程和儲存方法

準備一個oracle 的JDBC jar 包:ojdbc14_11g.jar

首先找到你的 oracle 安裝位置,例如:


1.建立一個JDBC資料庫連線工具類:

package com.test.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class JDBCUtils {

	private static String driver = "oracle.jdbc.OracleDriver";
	private static String url = "jdbc:oracle:thin:@127.0.0.1:1521:orcl";
	private static String user = "資料庫連線名";
	private static String password = "資料庫連線密碼";
	
	//註冊資料庫驅動
	static{
		try {
			Class.forName(driver);
		} catch (Exception e) {
			throw new ExceptionInInitializerError(e);
		}
	}
	
	/**
	 * 獲取資料庫連線
	 * @return
	 */
	public static Connection getConnection(){
		try {
			return DriverManager.getConnection(url,user,password);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 釋放資料庫連線資源
	 * @param conn
	 * @param st
	 * @param rs
	 */
	public static void release(Connection conn,Statement st,ResultSet rs){
		if (rs!=null) {
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}finally{
				rs = null;
			}
		}
		
		if (st!=null) {
			try {
				st.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}finally{
				st = null;
			}
		}
		
		if (conn!=null) {
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}finally{
				conn = null;
			}
		}
	}
}

2.呼叫 儲存過程:

package com.test.demo;

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

import org.junit.Test;

import oracle.jdbc.internal.OracleTypes;
import oracle.jdbc.oracore.OracleType;

import com.hwb.db.JDBCUtils;

/**
 * 呼叫儲存過程  一個輸入引數,多個 輸出引數
 * @author Administrator
 *
 */
public class ProcedureTest {

	/**
	 * create or replace procedure selectUserById(uid in number,
                                           uName out VARCHAR2,
                                           uAge out number,
                                           uSex out char)
	 */
	
	@Test
	public void testProcedure(){
		
		String sql = "{call selectUserById(?,?,?,?)}";
		
		Connection conn = null;
		CallableStatement call = null;
		try {
			//得到一個數據庫連線
			conn = JDBCUtils.getConnection();
			//通過連線創建出statement
			call = conn.prepareCall(sql);
			//對於in引數,賦值
			call.setInt(1, 2);  // (第幾個問號,要賦的值)
			//對out引數,宣告
			call.registerOutParameter(2, OracleTypes.VARCHAR);  //(第幾個問號,宣告的型別)
			call.registerOutParameter(3, OracleTypes.NUMBER);
			call.registerOutParameter(4, OracleTypes.CHAR);
			
			//執行呼叫
			call.execute();
			
			//取出結果
			String userName = call.getString(2);
			int userAge = call.getInt(3);
			String userSex = call.getString(4);
			
			System.out.println("使用者姓名:"+userName+"\n\t年齡:"+userAge+"\n\t性別:"+userSex);
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
                    //關閉連線,釋放資源
                   JDBCUtils.release(conn, call, null);
		}
		
	}
}

3.呼叫儲存方法:

package com.test.demo;

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

import oracle.jdbc.internal.OracleTypes;

import org.junit.Test;

import com.hwb.db.JDBCUtils;

/**
 * 呼叫儲存函式,一個輸入引數,一個輸出引數
 * @author Administrator
 *
 */
public class FunctionTest {

	/**
	 * create or replace function selectAge(eno in number)
		return number 
	 */
	@Test
	public void testFunction(){
		//{?= call <procedure-name>[<arg1>,<arg2>...]}
		String sql = "{call selectAge(?)}";
		Connection conn = null;
		CallableStatement call = null;
		try {
			//得到資料庫連線
			conn = JDBCUtils.getConnection();
			
			//通過資料庫連線建立statement
			call = conn.prepareCall(sql);
			
			//對於輸出引數,宣告
			call.registerOutParameter(1, OracleTypes.NUMBER);
			
			//對於輸入引數,賦值
			call.setInt(2, 3);
			
			//執行呼叫
			call.execute();
			
                        //獲取返回的結果
			int age = call.getInt(1);
			
			System.out.println("該使用者年齡:"+age);
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			JDBCUtils.release(conn, call, null);
		}
	}
}

4.呼叫儲存過程,一個輸入引數,返回一個查詢結果集合
package com.hwb.demo;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;




import org.junit.Test;

import oracle.jdbc.internal.OracleCallableStatement;
import oracle.jdbc.internal.OracleTypes;

import com.hwb.db.JDBCUtils;
/**
 * 儲存過程  一個輸入引數,返回一個查詢結果集合
 * @author Administrator
 *
 */
public class CursorTest {

	/**
	 * create or replace package Mypackage as
		procedure queryUserList(uid in number,userList out usercursor);
		end mypackage;
	 */
	@Test
	public void testCursor(){
		String sql = "{call Mypackage.queryUserList(?,?) }";
		
		Connection conn = null;
		CallableStatement call = null;
		ResultSet rs = null;
		try {
			//得到資料庫連線
			conn = JDBCUtils.getConnection();
			//通過資料庫連線建立statement
			call = conn.prepareCall(sql);
			
			//對於輸入引數,賦值
			call.setInt(1, 1);
			//對於輸出引數,宣告
			call.registerOutParameter(2, OracleTypes.CURSOR);
			//執行呼叫
			call.execute();
			//將CallableStatement 強轉成  OracleCallableStatement 用來獲取游標型別Cursor,並得到結果ResultSet
			rs = ((OracleCallableStatement)call).getCursor(2);
			//遍歷 ResultSet
			while (rs.next()) {
				//根據型別和列名取值
				int id = rs.getInt("user_id");    //括號內為 列名
				String user_name = rs.getString("user_name");
				int age = rs.getInt("user_age");
				String sex = rs.getString("user_sex");
				System.out.println("查詢到的使用者資訊:\n\tid:"+id+"\n\t姓名:"+user_name
						+"\n\t年齡:"+age+"\n\t性別:"+sex);
			}
			
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			JDBCUtils.release(conn, call, rs);
		}
	}
}