Sybase數據庫存儲過程我們經常會用到,下面就為您介紹JSP調用Sybase數據庫存儲過程返回記錄集的方法,希望對您學習使用Sybase數據庫存儲過程方面能有所幫助。
源代碼如下:
- <%@page contentType="text/Html;charset=GB2312"%>
- <%@page import="Javax.naming.*"%>
- <%@page import="Javax.sql.*"%>
- <%@page import="Java.sql.*"%>
- <%
- int n = 20;
- Connection conn = null;
- CallableStatement stmt = null;
- ResultSet rs = null;
- Context ctx = null;
- try {
- ctx = new InitialContext();
- //獲取連接池對象
- DataSource ds = (DataSource) ctx.lookup("JNDITEST_150");//JNDITEST_150是weblogic數據源的名字
- //String strSql_p="GetDataByPage @SqlStr=\"select * from bzlist\", @PageSize=20, @CurrentPage=1";
- String sproc="{?=call GetDataByPage(?,?,?)}";
- conn = ds.getConnection();
- stmt=conn.prepareCall(sproc);
- stmt.setString(2,"select * from bzlist");
- stmt.setInt(3,20);
- stmt.setInt(4,1);
- rs=stmt.executeQuery();
- out.print("<Table border=1><tr><th>分類號<th>標准編號<th>標准名稱<th>文件路徑</tr>");
- while(rs.next()){
- out.print("<tr><td>" + rs.getString("flh") + "</td>");
- out.print("<td>" + rs.getString("bzbh") + "</td>");
- out.print("<td>" + rs.getString("bzmc") + "</td>");
- out.print("<td>" + rs.getString("path") + rs.getString("filenames") + rs.getString("ext") + "</td></tr>");
- }
- out.print("</table>");
- rs.close();
- stmt.close();
- conn.close();
- }
- catch (Exception e) {
- System.out.println("錯誤如下:<br>" + e);
- e.printStackTrace();
- }
- %>
-----------------------------------------------------存儲過程如下:
- CREATE PROCEDURE GetDataByPage
- (
- --創建一個分頁讀取過程
- @SqlStr varchar(8000), --SQL語句
- @PageSize int, --每頁記錄數
- @CurrentPage int --當前頁數
- )
- AS
- DECLARE @FirstRec int, @LastRec int, @dt varchar(10) --頁起始行,頁結束行,生成臨時表的隨機數
- BEGIN
- SELECT @FirstRec = (@CurrentPage - 1) * @PageSize --計算頁起始行
- SELECT @LastRec = (@CurrentPage * @PageSize + 1) --計算頁結束行
- SELECT @dt= substring(convert(varchar,rand()),3,10) --一個字符型的隨機數
--將搜索結果放入臨時表中,表名隨機生成,在' FROM '前插入'INTO '+隨機臨時表名
- SELECT @SqlStr = stuff(@SqlStr, charindex(' FROM ',upper(@SqlStr)), 6 ,' INTO tempdb..Paging'+@dt+' FROM ')
- EXECUTE (@SqlStr)
--為臨時表增加id號
- SELECT @SqlStr = 'ALTER TABLE tempdb..Paging'+@dt+' ADD TEMPDB_ID numeric(10) IDENTITY PRIMARY KEY'
- EXECUTE (@SqlStr)
--計算臨時表中的記錄數
- --SELECT @SqlStr = 'SELECT Count(*) From tempdb..Paging'+@dt
- --EXECUTE (@SqlStr)
--選取記錄號在起始行和結束行中間的記錄
- SELECT @SqlStr = 'SELECT * FROM tempdb..Paging'+@dt+' WHERE TEMPDB_ID > '+convert(varchar,@FirstRec)+' and TEMPDB_ID < '+convert(varchar,@LastRec)
- EXECUTE (@SqlStr)
--刪除臨時表
- SELECT @SqlStr = 'DROP TABLE tempdb..Paging'+@dt
- EXECUTE (@SqlStr)
- END
----------------------------------------------------另外寫了個取記錄數的存儲過程:
- CREATE PROCEDURE xdztest.GetDataCount
- (
- @SqlStr varchar(8000)
- )
- AS
- BEGIN
- EXECUTE (@SqlStr)
- END