iT邦幫忙

DAY 13
3

JSP 學習分享系列 第 13

JSP 和 Hibernate 的設定教學第四篇

  • 分享至 

  • xImage
  •  

介紹如何刪除,修改
1.使用之前的範例再加工 ,只秀出修改部分,
主要是加刪除的超連結和修改的超連結
=== queryUserData3.jsp ===

	<tbody>
        <tr>
          <td><a href="modUserData.jsp?uid=<%=ud.getUserid()%>"><%=ud.getUserid()%></a></td>
          <td><%=ud.getUserpassword()%></td>
          <td><%=ud.getUsername()%></td>
          <td><a href="delUserData.jsp?uid=<%=ud.getUserid()%>">Delete</a></td>
        </tr>
      </tbody>

2.收到uid這個參數然後呼叫sess.delete ,記得要把private key都設進去
刪除後記得回到查詢畫面,可以用jsp:forward
=== delUserData.jsp ===

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="demo.*" %>
<%
  org.hibernate.Session sess = null;
  org.hibernate.SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
  sess = sessionFactory.openSession();
  try {
    org.hibernate.Transaction tx = sess.beginTransaction();
    String uid = request.getParameter("uid");
    if(uid!=null && !uid.equals("")){
      UserData ud = new UserData();
      ud.setUserid(uid);
      sess.delete(ud);
    }
    try {
      tx.commit();
    } catch (org.hibernate.TransactionException hix) {
      System.out.println("Something went wrong!"+hix);
    }
  } catch (Exception ex) {
    System.out.println(ex);
  } finally {
    sess.flush();
    sess.close();
  }
%>
  <jsp:forward page="queryUserData3.jsp"/>

3.修改的表格和新增幾乎一模一樣,只差private key需要唯讀
=== modUserData.jsp ===

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="demo.*" %>
<%
  org.hibernate.Session sess = null;
  org.hibernate.SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
  sess = sessionFactory.openSession();
  java.util.Iterator listud = null;
  org.hibernate.Query sqlq = null;
  UserData ud = null;
  try {
    String uid = request.getParameter("uid");
    if(uid!=null && !uid.equals("")){
      sqlq = sess.createQuery("from UserData where userid =:uid");
      sqlq.setString("uid", uid);
      listud = sqlq.iterate();
      ud = (UserData)listud.next();
    }
%>

  
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Modify UserData</title>
  
  
    <form name="mud" action="modData.jsp" method="POST">
      User ID : <input type="text" name="userid" value="<%=ud.getUserid()%>" readonly="readonly" /><br>
      User Password : <input type="password" name="userpwd" value="<%=ud.getUserpassword()%>" size="20" /><br>
      User Name : <input type="text" name="username" value="<%=ud.getUsername()%>" size="20"/><br>
      <input type="submit" value="Modify" name="modify" />
      <input type="submit" value="Cancel" name="cancel" />
    </form>
  

<%
  } catch (Exception ex) {
    System.out.println(ex);
  } finally {
    sess.flush();
    sess.close();
  }
%>

4.修改後記得回到查詢畫面,可以用jsp:forward
=== modData.jsp ===

	<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="demo.*" %>
<%
  org.hibernate.Session sess = null;
  org.hibernate.SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
  sess = sessionFactory.openSession();
  try {
    org.hibernate.Transaction tx = sess.beginTransaction();
    String uid = request.getParameter("userid");
    String uname = request.getParameter("username");
    String upwd = request.getParameter("userpwd");
    if(uid!=null && !uid.equals("")){
      UserData ud = new UserData();
      ud.setUserid(uid);
      ud.setUsername(uname);
      ud.setUserpassword(upwd);
      sess.saveOrUpdate(ud);
    }
    try {
      tx.commit();
    } catch (org.hibernate.TransactionException hix) {
      System.out.println("Something went wrong!"+hix);
    }
  } catch (Exception ex) {
    System.out.println(ex);
  } finally {
    sess.flush();
    sess.close();
  }
%>
  <jsp:forward page="queryUserData3.jsp"/>

上一篇
JSP 和 Hibernate 的設定教學第三篇
下一篇
JSP 如何上傳檔案
系列文
JSP 學習分享30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言