jsp之javaBean操作数据库之查,删,改

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/QQ736238785/article/details/65040198

本篇是上篇的延续。在敲代码的过程中,我自己遇到的问题是在修改操作的过程中的查询部分,没有想到要检验集合是否为空,导致要调用查询到的结果时为空。

图片如下:

查询:


删除:


修改:


代码:

Dao类(删除,修改,查询的方法)

 public  ResultSet Query(){
    	 PreparedStatement pst=null;
    	  ResultSet rs=null;
    	  UserCoon coon=new UserCoon();
    	  Connection coona=coon.getCoon();
    	  String sql_query="select * from user_table";
    	  try {
			pst=coona.prepareStatement(sql_query);
			rs=pst.executeQuery();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	 return rs;
     }
     public ResultSet Select(User usera){
    	 PreparedStatement pst=null;
    	 ResultSet rs=null;
   	     UserCoon coon=new UserCoon();
   	    Connection coona=coon.getCoon();
   	     String sql_select="select * from user_table where id=?";
   	  try {
			pst=coona.prepareStatement(sql_select);
			pst.setString(1,usera.getId());
			rs=pst.executeQuery();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
       }
	return rs;
   }
     public int Update(User usera){
    	 PreparedStatement pst=null;
    	 int rs=0;
   	     UserCoon coon=new UserCoon();
   	    Connection coona=coon.getCoon();
   	     String sql_update="update user_table set username=?,password=?,type=? where id=?";
   	  try {
			pst=coona.prepareStatement(sql_update);
			pst.setString(1,usera.getUsername());
			pst.setString(2,usera.getPassword());
			pst.setString(3,usera.getType());
			pst.setString(4,usera.getId());
			rs=pst.executeUpdate();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
       }
	return rs;
     }
     public int Delete(User usera){
    	 PreparedStatement pst=null;
    	 int rs=0;
   	     UserCoon coon=new UserCoon();
   	    Connection coona=coon.getCoon();
   	     String sql_delete="delete from user_table where id=?";
   	  try {
			pst=coona.prepareStatement(sql_delete);
			pst.setString(1,usera.getId());
			rs=pst.executeUpdate();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
       }
	return rs;
     }
查询页:queryBean.jsp

<%
  	  UserDao user=new UserDao();//实例化
       User  usera=new User();
       ResultSet rst=user.Query(); 
  %>
  <table border=1>
  <tr><td>用户名</td><td>密码</td><td>用户类型</td><td colspan="2" align="center">数据操作</td></tr>
  <%
  while(rst.next()){
  %>
  <tr><td><%=rst.getString("username")%></td><td><%=rst.getString("password")%></td><td><%=rst.getString("type")%></td><td><a href="deleteBean.jsp?id=<%=rst.getString("id")%>">删除操作</a></td><td><a href="UpdateBean.jsp?id=<%=rst.getString("id")%>>">修改操作</a></td></tr>
  <%
    }
  %>
   </table>
   <button><a href="login.jsp">添加数据</a></button>
删除页:deleteBean.jsp

<%
    UserDao user=new UserDao();//实例化
    User  usera=new User();
    usera.setId(request.getParameter("id"));
    int  rstd=user.Delete(usera);
%>
<jsp:forward page="queryBean.jsp"/>
修改页:

查询部分:updateBean.jsp

<%   
    request.setCharacterEncoding("utf-8");
    UserDao user=new UserDao();//实例化
    User  usera=new User();
    usera.setId(request.getParameter("id"));
    ResultSet rs=user.Select(usera);
    if(rs.next()){
%>
<form action="doUpdateBean.jsp?id=<%=rs.getString("id")%>" method="post">
用户名:<input type="text"  value="<%=rs.getString("username")%>" name="username"><br>
密 码:<input type="password" value="<%=rs.getString("password")%>" name="password"><br>
用户类型:<select name="type">
<option value="管理员">管理员</option>
<option value="普通用户">普通用户</option>
</select><br>
<input type="submit" value="修改">
<input type="reset" value="取消">
</form>
<%} %>
修改部分:doUpdateBean.jsp

% request.setCharacterEncoding("utf-8"); %>
<jsp:useBean id="use" class="nuc.test.user.User">
<jsp:setProperty name="use" property="*" />
</jsp:useBean>
<%
   UserDao user=new UserDao();//实例化
   User  usera=new User();
   usera.setId(request.getParameter("id"));
   int rstu=user.Update(use);
%>
<jsp:forward page="queryBean.jsp"/>






展开阅读全文

没有更多推荐了,返回首页