前段時間上傳瞭如何在jsp、java中調用oracle存儲過程的例子,如今把其中有用的部分摘錄出來以作參考。錯誤之處望見諒!javascript
上傳見:http://down.51cto.com/data/219597 java
--存儲過程:
--包:
create or replace package pck_recond
assql
------------------------------聲明遊標變量
type t_recond_cursor is ref cursor return icdmain.t_pub_commoninfo%rowtype; --弱遊標類型
type y_recond_cursor is ref cursor; --強遊標類型數據庫
-----------------------------返回錄音的存儲過程
procedure p_getrecond(i_serialno in icdmain.t_pub_commoninfo.serialno%type,
i_callerno in icdmain.t_pub_commoninfo.callerno%type,
i_accepterno in icdmain.t_pub_commoninfo.accepterno%type,
i_checkflag in icdmain.t_pub_commoninfo.checkflag%type,
o_dataset out y_recond_cursor --返回數據集
);安全
----------------------------更新質檢狀態的存儲過程session
procedure p_updaterecond(i_serialno in icdmain.t_pub_commoninfo.serialno%type,
i_remark in icdmain.t_pub_commoninfo.remark%type
);oracle
---------------------------存儲過程分頁
PROCEDURE p_query
(i_tableName in varchar2, --表名
i_curPage in out Number, --當前頁 in out
i_pageSize in out Number, --每頁顯示記錄條數 in out
i_totalRecords out Number, --總記錄數
i_totalPages out Number, --總頁數
o_cur out y_recond_cursor --返回數據集
);jsp
------------------------登錄
PROCEDURE p_login
(i_staffno in icdmain.t_pub_staff.staffno%type, --用戶no
i_password in icdmain.t_pub_staff.password%type, --密碼
o_staffno out icdmain.t_pub_staff.staffno%type,
o_msg out varchar2, ----返回錯誤消息
o_login out number,
o_staffstatus out number
);
----------------------退出
PROCEDURE P_LOGOUT
(i_staffno in icdmain.t_pub_staff.staffno%type --用戶no
);
end pck_recond;ide
------------------------------------------------------
--包體,僅寫出分頁的過程,因信息安全其他不便寫出:sqlserver
create or replace package body pck_recond
as
------------------------------分頁存儲過程
PROCEDURE p_query
(i_tableName in varchar2, --表名或試圖名
i_curPage in out Number, --當前頁
i_pageSize in out Number, --每頁顯示記錄條數
i_totalRecords out Number, --總記錄數
i_totalPages out Number, --總頁數
o_cur out y_recond_cursor) --返回的結果集
IS
v_sql VARCHAR2(1000) := ''; --sql語句
v_startRecord Number(4); --開始顯示的記錄條數
v_endRecord Number(4); --結束顯示的記錄條數
BEGIN
--記錄中總記錄條數
v_sql := 'SELECT TO_NUMBER(COUNT(*)) FROM ' || i_tableName || ' WHERE 1=1';
EXECUTE IMMEDIATE v_sql INTO i_totalRecords;
--驗證頁面記錄大小
IF i_pageSize < 0 THEN
i_pageSize := 0;
END IF;
--根據頁大小計算總頁數
IF MOD(i_totalRecords,i_pageSize) = 0 THEN
i_totalPages := ceil(i_totalRecords / i_pageSize);
ELSE
--i_totalPages := i_totalRecords / i_pageSize + 1;
i_totalPages := ceil(i_totalRecords / i_pageSize);
END IF;
--驗證頁號
IF i_curPage < 1 THEN
i_curPage := 1;
END IF;
IF i_curPage > i_totalPages THEN
i_curPage := ceil(i_totalPages);
END IF;
--實現分頁查詢
v_startRecord := (i_curPage - 1) * i_pageSize + 1;
v_endRecord := i_curPage * i_pageSize;
v_sql := 'SELECT * FROM (SELECT A.*, rownum r FROM ' ||
'(SELECT * FROM ' || i_tableName;
v_sql := v_sql || ') A WHERE rownum <= ' || v_endRecord || ') B WHERE r >= '|| v_startRecord;
DBMS_OUTPUT.put_line(v_sql);
OPEN o_cur FOR v_sql;
EXCEPTION
WHEN OTHERS THEN
OPEN o_cur FOR SELECT '1' FROM dual;
END p_query;
---package body 結束
end;
---------------------------------------------------------------------------------
--jsp頁面中調用(jsp、java寫的通常望諒解):
--登錄頁面:
<jsp:useBean id="Data" scope="request" class="javabean.DataBean"/>
<% request.setCharacterEncoding("GB2312");
Connection cn = Data.setConnectionOracle();
if(request.getParameter("staffno")!=null && request.getParameter("password2")!=null)
{
String staffno = new String(request.getParameter("staffno"));
String password2 = new String(request.getParameter("password2"));
CallableStatement stmt=cn.prepareCall("{call icdmain.pck_recond.p_login(?,?,?,?,?,?)}");
stmt.setString(1,staffno); //輸入參數
stmt.setString(2,password2);//輸入參數
stmt.registerOutParameter(3,oracle.jdbc.OracleTypes.VARCHAR); //輸出參數
stmt.registerOutParameter(4,oracle.jdbc.OracleTypes.VARCHAR); //輸出參數,錯誤消息
stmt.registerOutParameter(5,oracle.jdbc.OracleTypes.INTEGER); //輸出參數
stmt.registerOutParameter(6,oracle.jdbc.OracleTypes.INTEGER); //輸出參數
stmt.execute();
String v_staffno = stmt.getString(3);
session.setAttribute("staffno",v_staffno);
String msg = stmt.getString(4);
int login = stmt.getInt(5);
if (login==0)
out.println(msg);
else
response.sendRedirect("record/recordindex.jsp");
}
else
{
out.println("<tr><td colspan=4 align=center height=30>");
out.println("請輸入用戶名或密碼!");
out.println("</td>");
}
%>
----顯示頁面:
<jsp:useBean id="Data" scope="request" class="javabean.DataBean"/>
<% request.setCharacterEncoding("GB2312");
Connection cn = Data.setConnectionOracle();
if(request.getParameter("serialno")!=null || request.getParameter("callerno")!=null || request.getParameter("accepterno")!=null || request.getParameter("checkflag")!=null)
{
String serialno = new String(request.getParameter("serialno"));
String callerno = new String(request.getParameter("callerno"));
String accepterno = new String(request.getParameter("accepterno"));
String checkflag = new String(request.getParameter("checkflag"));
CallableStatement stmt=cn.prepareCall("{call icdmain.pck_recond.p_getrecond(?,?,?,?,?)}");
stmt.setString(1,serialno); //輸入參數
stmt.setString(2,callerno); //輸入參數
stmt.setString(3,accepterno); //輸入參數
stmt.setString(4,checkflag); //輸入參數
stmt.registerOutParameter(5,oracle.jdbc.OracleTypes.CURSOR); //返回遊標的參數
stmt.execute();
ResultSet rs=(ResultSet)stmt.getObject(5);
%>
--更新狀態頁面:
<%
Connection cn = Data.setConnectionOracle();
if(request.getParameter("remark")!=null)
{
String remark = new String( request.getParameter("remark"));
String serialno = (String)session.getValue("serialno");
CallableStatement stmt=cn.prepareCall("{call icdmain.pck_recond.p_updaterecond(?,?)}"); //2個輸入參數
stmt.setString(1,serialno); //送入參數
stmt.setString(2,remark); //送入參數
stmt.execute();
response.sendRedirect("recordindex.jsp");
}
else
out.println("<a href='upformrecord.jsp'>重填</a>");
%>
----分頁顯示頁面:
<jsp:useBean id="Data" scope="request" class="javabean.DataBean"/>
<%
int pagey; //從用戶那裏獲得的目標頁
String pages = request.getParameter("page");
if(pages==null)
{ pages="1";}
pagey = Integer.parseInt(pages);
//若是頁數小於1,那麼把頁數設置成1
if(pagey<1)
{ pagey=1; }
try{
Connection cn = Data.setConnectionOracle();
//分頁存儲過程
OracleCallableStatement stmt = (OracleCallableStatement)cn.prepareCall("{call icdmain.pck_recond.p_query(?,?,?,?,?,?)}");
stmt.setString(1,"v_recordfile"); //輸入參數,是個試圖
stmt.setInt(2,pagey); //輸入輸出參數
stmt.registerOutParameter(2,oracle.jdbc.OracleTypes.INTEGER); //輸入輸出參數 --當前頁
stmt.setInt(3,8); //輸入輸出參數,8是每頁顯示8條
stmt.registerOutParameter(3,oracle.jdbc.OracleTypes.INTEGER); //輸入輸出參數 --每頁顯示記錄條數
stmt.registerOutParameter(4,oracle.jdbc.OracleTypes.INTEGER); //輸出參數 --總記錄數
stmt.registerOutParameter(5,oracle.jdbc.OracleTypes.INTEGER); //輸出參數 --總頁數
stmt.registerOutParameter(6,oracle.jdbc.OracleTypes.CURSOR); //返回結果集 --返回的結果集
stmt.execute();
ResultSet rs = (ResultSet)stmt.getObject(6);
////////////////////////分頁
int currentPage = stmt.getInt(2);
int pageSize = stmt.getInt(3);
int recondSize = stmt.getInt(4);
int pageCount = stmt.getInt(5);
// 若是頁數大於總頁數,那麼設置成最大頁數
if(pagey>pageCount)
{ pagey=pageCount; }
%>
共 <font color=red><%=recondSize%></font> 條記錄,
每頁 <font color=red><%=pageSize%></font> 條,
共 <font color=red><%=pageCount%></font> 頁,
當前第 <font color=red><%=currentPage%></font> 頁
<a href="?page=1">首頁</a>
<a href="?page=<%=(currentPage-1)%>">上一頁</a>
<a href="?page=<%=(currentPage+1)%>">下一頁</a>
<a href="?page=<%=pageCount%>">尾頁</a>
<a href="../r_login.jsp" onclick="javascript:window.location.href="../r_login.jsp"; return false;"> </a>
----完
--------------------------------------------------------
---鏈接數據庫的通用java文件:
package javabean;
import java.sql.*;
public class DataBean {
public Connection cn = null;
public ResultSet rs = null;
public PreparedStatement ps=null;
public String dbs = null;
public String user = null;
public String pwd = null;
public DataBean() {}
public Connection setConnectionOdbc(String db) {
dbs = "jdbc:odbc" + db;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
cn = DriverManager.getConnection(dbs);
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
} catch (SQLException ex) {
ex.printStackTrace();
}
return cn;
}
public Connection setConnectionOracle() {
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
cn = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:ora10", "scott", "tiger");
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
} catch (SQLException ex) {
ex.printStackTrace();
}
return cn;
}
public Connection setConnectionSqlServer(String dbName)
{
dbs = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=" + dbName;
user="sa";
pwd="";
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
cn = DriverManager.getConnection(dbs,user,pwd);
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
} catch (SQLException ex) {
ex.printStackTrace();
}
return cn;
}
public ResultSet getQuery(String sql) {
ResultSet rs=null;
try {
ps = cn.prepareStatement(sql,ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
rs = ps.executeQuery();
} catch (SQLException ex) {
ex.printStackTrace();
}
return rs;
}
public void setModify(String sql) {
ps=null;
try {
ps = cn.prepareStatement(sql,ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
ps.executeUpdate();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
public static void showResultSet(ResultSet rs) {
try {
ResultSetMetaData rsmd = rs.getMetaData();
int col = rsmd.getColumnCount();
while (rs.next()) {
System.out.println("<tr>");
for (int i = 1; i <= col; i++) {
System.out.println("<td>" + rs.getString(i) + "</td>");
}
System.out.println("</tr>");
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public void stClose()
{
try
{
ps.close();
}
catch(SQLException e)
{
e.toString();
}
}
public void cnClose()
{
try
{
cn.close();
}
catch(SQLException e)
{
e.toString();
}
}
public static String convert(String str)
{
try
{
byte[] bytesstr=str.getBytes("ISO-8859-1");
return new String(bytesstr,"gb2312");
}
catch(Exception e )
{
e.toString();
}
return str;
}
}
---java文件完