JSP列表形式显示数据库中的数据 OracleCachedRowSet 实例

现在数据库中有一张用户表,希望用户在jsp页面中输入用户名和密码以及 用户类型,在servlet中插入数据库后,在另一个jsp页面中把数据库中所有的用户名和类型都以列表的形式列出来    可以用OracleCachedRowSet实现了ResultSet中的所有方法The oracle.jdbc.rowset.OracleCachedRowSet class is the Oracle implementation of CachedRowSet

servlet代码为:

import oracle.jdbc.rowset.OracleCachedRowSet;

PreparedStatement pUpd = con.prepareStatement("insert into myuser(username,password,user_type) values (?,?,?)" );
pUpd.setString(1,userName);
pUpd.setString(2,password);
int ty=Integer.parseInt(type);
pUpd.setInt(3,ty);
int numRows = pUpd.executeUpdate();
out.print("成功插入"+numRows+"行<br>");
out.print("从数据库中读取您的注册信息为: <br>");
pUpd = con.prepareStatement("select username,user_type from myuser " );
rs = pUpd.executeQuery(); 
   OracleCachedRowSet ors = new OracleCachedRowSet();
//将ResultSet中的数据封装到RowSet中
   ors.populate(rs);
  request.setAttribute("empRS",ors );

  RequestDispatcher rd;
      rd = getServletContext().getRequestDispatcher("/showResult.jsp");
      rd.forward(request,response); 

显示用户名和类型都以列表的形式的读取数据jsp为:showResult.jsp

     


<%@ page language="java" import="java.util.*,javax.sql.*,oracle.jdbc.rowset.OracleCachedRowSet" pageEncoding="utf-8"%>

 <body>
  <%
  OracleCachedRowSet empRS =(OracleCachedRowSet)request.getAttribute("empRS");
  
  %>

<table  cellspacing="0" width=”90%”>
    <tr>  <td>用户名</td> <td>类型</td>  </tr>
<%
  if (empRS != null) 
  while (empRS.next() ) 
  {
%>
  <tr>  
    <td><%= empRS.getString("userName")%></td> 
    <td><%= empRS.getString("user_type")%></td>  
  </tr>
<%
  }// end while
%>
</table>
  </body>


转载于:https://www.cnblogs.com/unflynaomi/p/4476855.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值