码迷,mamicode.com
首页 > 其他好文 > 详细

CallableStatement简单使用

时间:2014-04-28 10:25:43      阅读:461      评论:0      收藏:0      [点我收藏+]

标签:javaweb   callablestatement   java调用存储过程   java调用函数   

直接上存储过程、函数

--执行不带参数但带返回值的存储过程
CREATE OR REPLACE PROCEDURE proc_getUserCount(v_totalCount OUT NUMBER) AS
BEGIN
  SELECT COUNT(*) INTO v_totalCount FROM vote_user;
END;
--测试不带参数但带返回值的存储过程
DECLARE
  v_totalCount NUMBER;
BEGIN
  proc_getUserCount(v_totalCount);
  dbms_output.put_line(v_totalCount);
END;
--执行带参数返回值的存储过程
CREATE OR REPLACE PROCEDURE proc_getUserCountCondit(username IN VARCHAR2,
  v_totalCount OUT NUMBER) AS
BEGIN
  SELECT COUNT(*) INTO v_totalCount FROM vote_user
  WHERE vu_user_name LIKE ‘%‘ || username || ‘%‘;
END;
--测试带参数返回值的存储过程
DECLARE
  v_username VARCHAR2(20) := ‘accp‘;
  v_totalCount NUMBER;
BEGIN
  proc_getUserCountCondit(v_username,v_totalCount);
  dbms_output.put_line(v_totalCount);
END;

--执行返回值为游标的存储过程
CREATE OR REPLACE PROCEDURE proc_getUser(cursor_user OUT sys_refcursor) AS
BEGIN
  OPEN cursor_user
  FOR
    SELECT vu_user_name,vu_password FROM vote_user;
END;
--测试执行返回值为游标的存储过程
DECLARE
  cursor_user SYS_REFCURSOR;
  v_username VARCHAR2(20);
  v_password VARCHAR2(20);
BEGIN
  proc_getUser(cursor_user);
  LOOP
    FETCH cursor_user INTO v_username,v_password;
    EXIT WHEN cursor_user%NOTFOUND;
    dbms_output.put_line(‘用户名:‘ || v_username || ‘,密码:‘ || v_password);
  END LOOP;
END;
--执行函数
CREATE OR REPLACE FUNCTION func_getUserName(user_id VARCHAR2)
  RETURN VARCHAR2
  IS username VARCHAR2(20);
BEGIN
    SELECT vu_user_name INTO username FROM vote_user 
    WHERE vu_user_id = user_id;
    RETURN username;
    EXCEPTION
      WHEN no_data_found THEN
        RETURN ‘雇员编号未找到‘;
END;
--测试函数
DECLARE
  username VARCHAR2(20);
BEGIN
  username := func_getUserName(‘accp2‘);
  dbms_output.put_line(‘用户名:‘ || username);
END;
Java示例代码

/**
 * cn.jbit.news.test.Demo2
 * 2014-4-27
 * gyy
 */
package cn.jbit.news.test;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.List;

import oracle.jdbc.OracleTypes;

import cn.jbit.news.entity.User;
import cn.jbit.news.util.ConfigManager;

public class Demo2 {

	private ConfigManager config = ConfigManager.getInstance();

	// 执行不带参数但是有返回值的存储过程
	public int getUserCount() {
		int rowsCount = 0;

		try {
			Class.forName(config.getString("jdbc.driver_class"));
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		Connection conn = null;
		CallableStatement cstmt = null;
		try {
			conn = DriverManager.getConnection(
					config.getString("jdbc.connection.url"),
					config.getString("jdbc.connection.username"),
					config.getString("jdbc.connection.password"));
			String sql = "{call proc_getUserCount(?)}";
			// 创建CallableStatement对象
			cstmt = conn.prepareCall(sql);
			// 注册输出参数
			cstmt.registerOutParameter(1, Types.INTEGER);
			// 执行存储过程
			cstmt.execute();
			rowsCount = cstmt.getInt(1);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return rowsCount;
	}

	// 执行带参数带返回值的存储过程
	public int getUserCountByName(String username) {
		int rowsCount = 0;

		try {
			Class.forName(config.getString("jdbc.driver_class"));
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		Connection conn = null;
		CallableStatement cstmt = null;
		try {
			conn = DriverManager.getConnection(
					config.getString("jdbc.connection.url"),
					config.getString("jdbc.connection.username"),
					config.getString("jdbc.connection.password"));
			String sql = "{call proc_getUserCountCondit(?,?)}";
			// 创建CallableStatement对象
			cstmt = conn.prepareCall(sql);
			// 输入参数赋值
			cstmt.setString(1, username);
			// 注册输出参数
			cstmt.registerOutParameter(2, Types.INTEGER);
			// 执行存储过程
			cstmt.execute();
			rowsCount = cstmt.getInt(2);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return rowsCount;
	}

	// 执行返回值为游标的存储过程执行返回值为游标的存储过程
	public List<User> getUserListByProc() {
		List<User> userList = null;

		try {
			Class.forName(config.getString("jdbc.driver_class"));
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		Connection conn = null;
		CallableStatement cstmt = null;
		ResultSet rs = null;
		try {
			conn = DriverManager.getConnection(
					config.getString("jdbc.connection.url"),
					config.getString("jdbc.connection.username"),
					config.getString("jdbc.connection.password"));
			String sql = "{call PROC_GETUSER(?)}";
			// 创建CallableStatement对象
			cstmt = conn.prepareCall(sql);
			// 注册输出参数
			cstmt.registerOutParameter(1, OracleTypes.CURSOR);
			// 执行存储过程
			cstmt.execute();
			rs = (ResultSet) cstmt.getObject(1);
			userList = new ArrayList<User>();
			while (rs.next()) {
				String username = rs.getString("VU_USER_NAME");
				String password = rs.getString("VU_PASSWORD");
				User user = new User();
				user.setUsername(username);
				user.setPassword(password);
				userList.add(user);// 添加用户
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return userList;
	}

	// 函数
	public String getUserNameById(String userId) {
		String username = "";

		try {
			Class.forName(config.getString("jdbc.driver_class"));
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		Connection conn = null;
		CallableStatement cstmt = null;
		try {
			conn = DriverManager.getConnection(
					config.getString("jdbc.connection.url"),
					config.getString("jdbc.connection.username"),
					config.getString("jdbc.connection.password"));
			String sql = "{? = call FUNC_GETUSERNAME(?)}";
			// 创建CallableStatement对象
			cstmt = conn.prepareCall(sql);
			// 输入参数赋值
			cstmt.setString(2, userId);
			// 注册输出参数
			cstmt.registerOutParameter(1, Types.VARCHAR);
			// 执行存储过程
			cstmt.execute();
			username =  cstmt.getString(1);
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return username;
	}

	public static void main(String[] args) {
		Demo2 d = new Demo2();
		System.out.println("用户人数:" + d.getUserCount());
		System.out.println("模糊查询-------用户人数:" + d.getUserCountByName("accp"));
		List<User> userList = d.getUserListByProc();
		for (User user : userList) {
			System.out.println("用户名:" + user.getUsername() + ",密码:"
					+ user.getPassword());
		}
		System.out.println(d.getUserNameById("accp"));
	}
}

CallableStatement简单使用

标签:javaweb   callablestatement   java调用存储过程   java调用函数   

原文地址:http://blog.csdn.net/com185272358/article/details/24606403

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!