7、使用CallableStatement接口调用存储过程
生活随笔
收集整理的這篇文章主要介紹了
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)BEGINSELECT 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));} }運行結果:
總結
以上是生活随笔為你收集整理的7、使用CallableStatement接口调用存储过程的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 6、处理大数据对象(CLOB 数据小说+
- 下一篇: 8、使用元数据(描述数据属性的信息)分析