• CallableStatement简单使用


    直接上存储过程、函数

    --运行不带參数但带返回值的存储过程
    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"));
    	}
    }

  • 相关阅读:
    20155208徐子涵 《网络对抗》Exp1 PC平台逆向破解
    20155208 《信息安全系统设计基础》课程总结
    20155208 《信息安全系统设计基础》第十四周学习总结
    20155208《信息安全系统设计基础》第十三周学习总结
    2017-2018-1 20155208 实验五 通讯协议设计
    2017-2018-1 20155208 《信息安全系统设计基础》第十一周学习总结
    20155207王雪纯 Exp2 后门原理与实践
    # 20155207王雪纯 实验一 逆向与Bof基础
    20155207 《信息安全系统设计基础》课程总结
    2017-2018-1 20155207 《信息安全系统设计基础》第14周学习总结
  • 原文地址:https://www.cnblogs.com/gcczhongduan/p/3990960.html
Copyright © 2020-2023  润新知