日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時間:2025/3/20 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 7、使用CallableStatement接口调用存储过程 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

第一節(jié):CallableStatement 接口的引入

  • CallableStatement 主要是調(diào)用數(shù)據(jù)庫中的存儲過程,CallableStatement 也是 Statement
    接口的子接口。在使用CallableStatement 時可以接收存儲過程的返回值。

第二節(jié):使用 CallableStatement 接口調(diào)用存儲過程

void registerOutParameter(int parameterIndex, int sqlType)

按順序位置 parameterIndex 將 OUT 參數(shù)注冊為 JDBC 類型 sqlType。

實例演示:

創(chuàng)建存儲過程,通過圖書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;

實例:在程序中調(diào)用存儲過程

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";/*** 獲取數(shù)據(jù)庫連接* @return* @throws Exception*/public Connection getCon() throws Exception{Class.forName(jdbcName);Connection con=DriverManager.getConnection(dbUrl,dbUserName,dbPassword); return con;}/*** 關(guān)閉連接* @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();/*** 調(diào)用存儲過程,通過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);//設(shè)置第一個參數(shù)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));} }

運行結(jié)果:

總結(jié)

以上是生活随笔為你收集整理的7、使用CallableStatement接口调用存储过程的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。