CallableStatement 接口的引入
CallableStatement 主要是调用数据库中的存储过程,CallableStatement 也是 Statement 接口的子接口。在使用 CallableStatement 时可以接收存储过程的返回值。
使用 CallableStatement 接口调用存储过程
void registerOutParameter(int parameterIndex, int sqlType)
按顺序位置 parameterIndex 将 OUT 参数注册为 JDBC 类型 sqlType。
数据库
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.Types;
import com.cn.zj.JDBCUtil.DbUtil;
public class demo {
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.executeQuery();
String bookName=cstmt.getString("bN"); //获取返回值
dbUtil.close(cstmt, con);
return bookName;
}
public static void main(String[] args)throws Exception {
System.out.println("图书名称是:"+getBookNameById(1));
}
}
========================================
工具类DbUtil.java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
/**
* 通用方法
* @author Administrator
*
*/
public class DbUtil {
//驱动名称
private static String jdbcName="com.mysql.jdbc.Driver";
//mysql数据库地址
private static String dbUrl="jdbc:mysql://localhost:3306/db_book?useUnicode=true&characterEncoding=UTF-8";
//用户名
private static String dbUserName="root";
//密码
private static String dbPassword="root";
//获取数据库连接的方法
public Connection getCon() throws Exception{
Class.forName(jdbcName); //加载驱动
Connection con=DriverManager.getConnection(dbUrl, dbUserName, dbPassword);
return con;
}
//关闭数据库连接
public void close(Statement stmt,Connection con)throws Exception{
if(stmt!=null){
stmt.close();
if(con!=null){
con.close();
}
}
}
}
Book.java
import java.io.File;
/**
* 图书模型
* @author Administrator
*
*/
public class Book {
private int id;
private String bookName;
private float price;
private String author;
private int bookTypeId;
private File context; //使用流
private File pic; //图片
//构造方法 Source+fields
public Book(String bookName, float price, String author, int bookTypeId) {
super();
this.bookName = bookName;
this.price = price;
this.author = author;
this.bookTypeId = bookTypeId;
}
//更新数据要包括ID,重载构造方法
public Book(int id, String bookName, float price, String author, int bookTypeId) {
super();
this.id = id;
this.bookName = bookName;
this.price = price;
this.author = author;
this.bookTypeId = bookTypeId;
}
public Book(String bookName, float price, String author, int bookTypeId, File context) {
super();
this.bookName = bookName;
this.price = price;
this.author = author;
this.bookTypeId = bookTypeId;
this.context = context;
}
public Book(String bookName, float price, String author, int bookTypeId, File context, File pic) {
super();
this.bookName = bookName;
this.price = price;
this.author = author;
this.bookTypeId = bookTypeId;
this.context = context;
this.pic = pic;
}
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 float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public int getBookTypeId() {
return bookTypeId;
}
public void setBookTypeId(int bookTypeId) {
this.bookTypeId = bookTypeId;
}
public File getContext() {
return context;
}
public void setContext(File context) {
this.context = context;
}
public File getPic() {
return pic;
}
public void setPic(File pic) {
this.pic = pic;
}
@Override
public String toString() {
return "Book [id=" + id + ", bookName=" + bookName + ", price=" + price + ", author=" + author + ", bookTypeId="
+ bookTypeId + "]";
}
}