日期:2014-05-16 浏览次数:20530 次
源代码如下:
<%@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
?