7、使用CallableStatement接口调用存储过程

第一节:CallableStatement 接口的引入

  • CallableStatement 主要是调用数据库中的存储过程,CallableStatement 也是 Statement
    接口的子接口。在使用CallableStatement 时可以接收存储过程的返回值。

第二节:使用 CallableStatement 接口调用存储过程

void registerOutParameter(int parameterIndex, int sqlType)

按顺序位置 parameterIndex 将 OUT 参数注册为 JDBC 类型 sqlType。

实例演示:

创建存储过程,通过图书id获取图书名bookName

DELIMITER &&
CREATE PROCEDURE pro_getBookNameById(IN bookId INT,OUT bN VARCHAR(100) character set utf8)
 BEGIN
	SELECT bookName INTO bn FROM books WHERE id=bookId;
 END 
&&
DELIMITER ;
CALL pro_getBookNameById(10,@bookName);
SELECT @bookName;

实例:在程序中调用存储过程

public class Books {

	private int id;
	private String bookName;
	private String author;
	private float price;
	private File content;
	private File illustration;
	
	public Books() {
		super();
		// TODO Auto-generated constructor stub
	}
	public Books(String bookName, String author, float price, File content) {
		super();
		this.bookName = bookName;
		this.author = author;
		this.price = price;
		this.content = content;
	}
	
	
	public Books(String bookName, String author, float price, File content, File illustration) {
		super();
		this.bookName = bookName;
		this.author = author;
		this.price = price;
		this.content = content;
		this.illustration = illustration;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getBookName() {
		return bookName;
	}
	public void setBookName(String bookName) {
		this.bookName = bookName;
	}
	public String getAuthor() {
		return author;
	}
	public void setAuthor(String author) {
		this.author = author;
	}
	public float getPrice() {
		return price;
	}
	public void setPrice(float price) {
		this.price = price;
	}
	public File getContent() {
		return content;
	}
	public void setContent(File content) {
		this.content = content;
	}
	public File getIllustration() {
		return illustration;
	}
	public void setIllustration(File illustration) {
		this.illustration = illustration;
	}
	
	
}
public class DbUtil {

	private static String dbUrl="jdbc:mysql://localhost:3306/book?useUnicode=true&characterEncoding=UTF-8";
	private static String dbUserName="root";
	private static String dbPassword="root";
	private static String jdbcName="com.mysql.jdbc.Driver";
	
	/**
	 * 获取数据库连接
	 * @return
	 * @throws Exception
	 */
	public Connection getCon() throws Exception{
		Class.forName(jdbcName);
		Connection con=DriverManager.getConnection(dbUrl,dbUserName,dbPassword);		
		return con;
	}
	
	/**
	 * 关闭连接
	 * @param con
	 * @throws Exception
	 */
	public void close(Statement stmt,Connection con)throws Exception{
		if(stmt!=null){
			stmt.close();
			if(con!=null){
				con.close();
			}
		}
		
	}
}
public class jdbcTest {
	private static DbUtil dbUtil=new DbUtil();
	
	/**
	 * 调用存储过程,通过id查询bookName
	 * @param id
	 * @return
	 * @throws Exception
	 */
	private static String getBookNameById(int id)throws Exception{
		Connection con=dbUtil.getCon();
		String sql="{CALL pro_getBookNameById(?,?)}";
		CallableStatement cstmt=con.prepareCall(sql);
		cstmt.setInt(1, id);//设置第一个参数
		cstmt.registerOutParameter(2, Types.VARCHAR);
		cstmt.execute();
		String bookName=cstmt.getString("bN");//获取返回值
		dbUtil.close(cstmt, con);
		return bookName;
		
	}
	public static void main(String[] args)throws Exception {
		System.out.println("图书名:"+getBookNameById(26));
	}
}

运行结果:
在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值