JDBC預存程序的調用,JDBC預存程序調用
一. JDBC預存程序的調用
(1)預存程序是用SQL語句和控制流程語句等編寫的一段程式碼,在建立時已被編譯成機器代碼並儲存在資料庫中供用戶端調用。
預存程序有以下優點:
1.所產生的機器代碼被永久儲存在資料庫中,用戶端調用時不需要重新編譯,執行起來效率要高一些。
2.預存程序的網路使用效率比等效的SQL 陳述式要高。
(2)JDBC通過java.sql.CallableStatement介面調用資料庫伺服器中的預存程序;
(3)CallableStatement介面繼承了PreparedStatement,PreparedStatement中常用的方法也適用於CallableStatement介面,
介面中常用的方法有:
1.public void setString(int n,String x) throws SQLException
將一個字串類型的資料值x寫入預存程序調用命令的第n個“?”號處,代替“?”,n為先行編譯語句中“?”的序號,第一個“?”的序號為1。
2.public ResultSet executeQuery() throws SQLException
執行一個會返回ResultSet結果集的預存程序。
3.public boolean execute() throws SQLException
通用的預存程序執行方法
4.public void registerOutParameter(int n,int sqlType) throws SQLException
將預存程序調用命令{call …}中第n個位置處的“?”參數註冊聲明為輸出(OUT)參數,並定義返回資料的類型。返回資料類型SqlType可以用java.sql.Types類中的符號常量表達。
5.public int getInt(int n) throws SQLException
讀取預存程序調用命令中“?”位置處的一個整數返回值,n為“?”號在預存程序調用命令中的序號。
二.執行個體解說
(1).調用一個能夠返回一個ResulSet結果集的預存程序,對titles表的書名欄位進行模糊查詢,返回書名、類型、單價資料。操作步驟如下:
第1步:在pubs中建立一個名為exam4的預存程序完成查詢。(在資料庫中SQL Server中完成)
use pubsgocreate proc exam4 @key varchar(50)asbegin select title,type,price from titles where title like @keyend
第2步:建立一個名為exam608.jsp的頁面,在此JSP網頁中調用此預存程序。
<%@ page contentType="text/html; charset=gb2312" language="java"import="java.sql.*" errorPage=""%><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><title>無標題文檔</title></head><body><%Connection con = null;CallableStatement st = null;ResultSet rs = null;try {Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");String url = "jdbc:sqlserver://localhost:1433;databaseName=pubs;user=sa;password=";con = DriverManager.getConnection(url);String sql = "{call exam4(?)}"; //效果往這來看!!!st = con.prepareCall(sql);st.setString(1, "%the%");rs = st.executeQuery();while (rs.next()) {out.print(rs.getString(1));out.print(rs.getString(2));out.print(rs.getString(3));out.print("<br>");}} catch (Exception e) {out.print("資料庫操作出錯:" + e);} finally {if (rs != null)rs.close();if (st != null)st.close();if (con != null)con.close();}%></body></html>
(2)調用一個預存程序,給titles表添加一條新記錄,新記錄中至少包含書號、書名、類型、單價、出版日期五個資料。用預存程序實現。
操作步驟如下:
第1步:在pubs中建立一個名為newRecord的預存程序,輸入以下代碼:
use pubsgocreate proc newRecord @title_id varchar(50), @title varchar(100), @type varchar(30), @price money, @pubdate datetimeasbegin insert into titles(title_id,title,type,price,pubdate) values(@title_id,@title,@type,@price,@pubdate) end
第2步:建立一個名為exam609.jsp的JSP表單頁面供使用者輸入圖書資訊。
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><title>無標題文檔</title></head><body><form id="form1" name="form1" method="post" action="exam610.jsp"> 請輸入書號(如AD3344): <label> <input name="book_id" type="text" id="book_id" /> </label> <p>請輸入書名(如Java程式設計): <label> <input name="book_name" type="text" id="book_name" /> </label> </p> <p>請選擇書的類型: <label> <select name="book_type" id="book_type"> <option value="business">business</option> <option value="psychology">psychology</option> <option value="trad_cook">trad_cook</option> <option value="popular_comp">popular_comp</option> </select> </label> </p> <p>請輸入書的單價(如45.3): <label> <input name="book_price" type="text" id="book_price" /> </label> </p> <p>請輸入圖書的出版日期(如2004-3-6): <label> <input name="pub_date" type="text" id="pub_date" /> </label> </p> <p> <label> <input type="submit" name="Submit" value="提交" /> </label> </p></form></body></html>
第3步:定義exam610.jsp讀取表單中的資料並寫入資料庫中。
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %><html><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312"><title>無標題文檔</title></head><body><% String book_id=request.getParameter("book_id"); String book_name=request.getParameter("book_name"); String book_type=request.getParameter("book_type"); String book_price=request.getParameter("book_price"); String pub_date=request.getParameter("pub_date"); Connection con=null; CallableStatement st =null;try{ Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); String url="jdbc:sqlserver://localhost:1433;databaseName=pubs;user=sa;password="; con = DriverManager.getConnection(url); String sql="{call newRecord(?,?,?,?,?)}"; st=con.prepareCall(sql); st.setString(1,book_id); st.setString(2,book_name); st.setString(3,book_type); st.setDouble(4,Double.parseDouble(book_price)); st.setString(5,pub_date); st.execute(); out.println("成功加入記錄,請用查詢分析器驗證");} catch(Exception e) { out.println(e); } finally { if(st != null ) {st.close();}if(con != null ) {con.close();} }%></body></html>
對於jdbc調用預存程序的問題
那說明sun的驅動比SQLServerDriver的相容性好啊
你要用jdbc,就得按標準的jdbc來跑,execute就夠了,別用executeQuery……
幫忙看下,jdbc調用預存程序這程式是什?
問題還真多啊!
1.是不是必須先建個預存程序了才可以操作這程式? A:是
2.幫我把在程式中注釋的標號做個解釋看是什麼意思?
1.SCOTT"."FIND_EMP_INFO這個意思是說為scott使用者建的預存程序嗎? A:是
2. v_empno in emp.empno%type什麼意思?in在當中是什麼意思?%type到底是什麼意思結合這段程式是什麼意思? A:參數v_empno 為輸入參數,類型為表 emp的欄位 empno 的類型
3. v_ename out emp.ename%type是什麼意思:out在當中是什麼意思? A:輸出參數
4. empno=v_empno怎麼可以相等啊? A: 看到前邊有一個 where 了嗎,是說當 表中某一行(或者多行)的值 等於 參數 v_empno時,取出此行(或多行資料)
5.call我查api是一種固定寫法?對嘛? A:可以吧,我沒有用花括弧
6.API中,傳遞in參數還有說什麼註冊out是什麼意思?另外把(1, 7902)傳遞或者說設定到哪裡去了? A:根據預存程序的定義,告訴java各參數的情況,in表示輸入參數 (1, 7902)表示,第一個參數值為7902(就是String sql = "{call find_emp_info(?,?,?,?)}" 中的第一個問號)
7. 註冊out參數說的是什麼意思?調用registerOutParameter(3, Types.VARCHAR);方法時,裡面數字2或者3是什麼意思?數字後面類型是什麼意思? A:同上,輸出參數,第二、第三個參數,Types.VARCHAR表示資料類型是varchar
cstm.getString(2))意思是什嗎?2代表或者3代表什麼意思? A:取出第二個參數(out類型)的值
3. 幫我看下怎麼得出這運行結果了,我不清楚這結果怎麼來的。
System.out.println("查詢結果: " + cstm.getString(2));//取出OUT參數的值
System.out.println(" " + cstm.getString(3));
System.out.println(" " + cstm.getFloat(4));
這3行代碼,分別取出參數值,並列印出來。