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"));
  }
}

摘自:http://www.tuicool.com/articles/ie2uQvZ

时间: 2024-08-28 13:24:07

CallableStatement的相关文章

jdbc java数据库连接 5)CallableStatement 接口

CallableStatement执行存储过程(也是预编译语言) 首先在sql中执行以下带有输入参数的代码: 1 DELIMITER $ 2 CREATE PROCEDURE pro_findById(IN sid INT) 3 BEGIN 4 SELECT * FROM person WHERE id = sid; 5 END $ 那么,这条语句的存储过程代码就是 CALL pro_findById (?); 使用CallableStatement来执行: 1 /** 2 带有输入参数的存储语

使用CallableStatement的用法

package Test; import java.sql.*; public class Test7 { public static void main(String[] args) { Connection con=null; CallableStatement csta=null; try { Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); con=DriverManager.getConnection

使用CallableStatement接口调用存储过程

直接上下代码: 1 package com.learn.jdbc.chap07; 2 3 import java.sql.CallableStatement; 4 import java.sql.Connection; 5 import java.sql.Types; 6 7 import com.learn.jdbc.util.DbUtil; 8 9 /** 10 * 使用CallableStatement接口调用存储过程 11 * @author Administrator 12 * 13

Statement、PreparedStatemnt、CallableStatement

第一.Statement(Statement代表一个特定的容器,来对一个特定的数据库执行语句) * 执行查询的方法 Statement=Connection.createStatement();//创建执行sql的句柄对象ResultSet=Statement.executeQuery(sql);//执行查询,返回结果集 * 执行增删改的方法 Statement=Connection.createStatement();//创建执行sql的句柄对象 int t=Statement.execute

JDBC PreparedStatement ,CallableStatement,以及事务,回滚举例

程序中用到的类,文件,jar 代码: 1,文件:db.properties文件内容 user=rootpassword=123url=jdbc:mysql:///student_dbdriver=com.mysql.jdbc.Driveraaa 2,类Utils.class import java.io.InputStream;import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException

使用命名参数处理 CallableStatement

简介:JDBC 中的语句处理 在 JDBC 应用程序中,JDBC 语句对象用于将 SQL 语句发送到数据库服务器.一个语句对象与一个连接相关联,应用程序与数据库服务器之间的通信由语句对象来处理. JDBC 中有三种类型的语句对象: 常规语句(General statement) 预置语句(Prepared statement) 可调用语句(Callable statement) 语句对象与一个连接相关联,所以要创建一个语句对象,首先应该建立一个数据库连接. 创建连接 清单 1 中的代码示例演示了

JDBC之Statement,PreparedStatement,CallableStatement的区别

Statement. PreparedStatement .CallableStatement 区别和联系 1. Statement.PreparedStatement和CallableStatement都是接口(interface). 2. Statement继承自Wrapper.PreparedStatement继承自Statement.CallableStatement继承自PreparedStatement. 3. Statement接口提供了执行语句和获取结果的基本方法:     Pr

说说Statement、PreparedStatement和CallableStatement的异同(转)

1.Statement.PreparedStatement和CallableStatement都是接口(interface). 2.Statement继承自Wrapper.PreparedStatement继承自Statement.CallableStatement继承自PreparedStatement. 3. Statement接口提供了执行语句和获取结果的基本方法: PreparedStatement接口添加了处理 IN 参数的方法: CallableStatement接口添加了处理 OU

老调重弹:JDBC系列 之 存储过程 CallableStatement(创建和使用)

前言 最近在研究Mybatis框架,由于该框架基于JDBC,想要很好地理解和学习Mybatis,必须要对JDBC有较深入的了解.所以便把JDBC 这个东东翻出来,老调重弹,好好总结一番,作为自己的笔记,也是给读者一个参考--- 本文主要通过 使用JDBC创建存储过程 和使用JDBC调用存储过程两部分 阐述JDBC 对存储过程的支持.本文将在Oracle数据库下创建一个可以表示岗位信息的基本表Jobs为例, 然后通过存储过程对这个Jobs表进行各种操作.表JOBS的建表语句如下: -- Creat