2017-05-24 45 views
1

歷史: History I. History II.如何在JSP中的德比數據庫中插入和刪除值? [3]

所以問題還在這裏。如果我選擇某人並按下刪除按鈕,則不要從數據庫中刪除其他人創建的新空記錄。

的client.java:

public class client implements DatabaseConnection{ 

private static Connection conn = null; 

private static void createConnection(){ 
try { 
    conn = DriverManager.getConnection(URL, USER, PASSWORD); 
} catch (SQLException ex) { 
    Logger.getLogger(client.class.getName()).log(Level.SEVERE, null, ex); 
} 
} 

private static void closeConnection(){ 
if (conn != null){ 
    try { 
    conn.close(); 
    } catch (SQLException ex) { 
    Logger.getLogger(client.class.getName()).log(Level.SEVERE, null, ex); 
    } 
} 
} 

public List clientList(){ 
    createConnection(); 
    List list=new ArrayList(); 
    try { 
     Statement stmt=conn.createStatement(); 
     ResultSet rs=stmt.executeQuery("SELECT * FROM customer"); 
     while(rs.next()){ 
      **list.add(rs.getString("ID"));** 
      list.add(rs.getString("CNAME")); 
      list.add(rs.getString("ADDRESS")); 
      list.add(rs.getString("PHONENUMBER")); 
     } 
     stmt.close(); 
    } catch (Exception e) { 
     e.printStackTrace(System.err); 
    } 
    return list; 
} 

public void newClient(String name, String address, String phoneNumber) 
throws SQLException{ 
    PreparedStatement ps = null; 
    try { 
     createConnection(); 
     String insert="INSERT INTO CUSTOMER(CNAME,ADDRESS, PHONENUMBER) 
VALUES(?,?,?)"; 
     ps=conn.prepareStatement(insert); 
     ps.setString(1, name); 
     ps.setString(2, address); 
     ps.setString(3, phoneNumber); 
     ps.executeUpdate(); 
     ps.close(); 
    } catch (Exception e) { 
     e.printStackTrace(System.err); 
    } 
    finally { 
     ps.close(); 
     closeConnection(); 
    } 
} 
public void deleteClient(String ID){ 
    try { 
     createConnection(); 
     String delete="DELETE FROM CUSTOMER WHERE ID=?"; 
     PreparedStatement ps=conn.prepareStatement(delete); 
     ps.setString(1, ID); 
     ps.executeUpdate(); 
    } catch (Exception e) { 
     e.printStackTrace(System.err); 
    } 
    **finally { 
    closeConnection(); 
}** 
} 

} 

中的index.jsp:

<jsp:useBean id="client" class="database.client" scope="page" /> 
<!DOCTYPE html> 
<html> 
<head> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
    <title>JSP Page</title> 
</head> 
<body bgcolor="lightgrey"> 
    <% 
     String ID=request.getParameter("ID"); 
     String NAME=request.getParameter("CNAME"); 
     String ADDRESS=request.getParameter("ADDRESS"); 
     String PHONENUMBER=request.getParameter("PHONENUMBER"); 
    %> 
    <form method="post" action=""> 
    <table border="0" align="left"> 
     <th colspan="2" align="center" style="color: brown">Field</th> 
     <tr> 
      <td>Name:</td> 
      <td><input type="text" name="CNAME" style="background- 
color:beige"/></td> 
     </tr> 
     <tr> 
      <td>Address?</td> 
      <td><input type="text" name="ADDRESS" style="background- 
color:beige"/></td> 
     </tr> 
     <tr> 
      <td>PhoneNumber:</td> 
      <td><input type="text" name="PHONENUMBER" style="background- 
color:beige"/></td> 
     </tr> 
      <input type="submit" name="OK" onclick=" 
       <% 
       if(NAME!=null && ADDRESS!=null && PHONENUMBER!=null){ 
        client.newClient(NAME, ADDRESS, PHONENUMBER); 
       } 
       %>" value="OK"/> 
      <input type="submit" name="Cancel" onclick=" 
       <% 
       //nothing 
       %>" value="Cancel"/> 
      <input type="submit" name="Delete" onclick=" 
<%client.deleteClient(ID);%>" value="Delete"/> 
    </table> 
    <table border="2"> 
     <th colspan="4" align="center" bgcolor="orange">Clients</th> 
     <tr bgcolor="silver" align="center"> 
      <td>&nbsp;</td> 
      **<td>ID</td>** 
      <td>Name</td> 
      <td>Address</td> 
      <td>PhoneNumber</td> 
     </tr> 
     <% 
      List list=client.clientList(); 
      Iterator it=list.iterator(); 

      while(it.hasNext()){ 
       out.print("<tr bgcolor='lightgreen'>"); 
       out.print("<td>"); 
       **ID**=(String)it.next(); 
       out.print("<input type='radio' name='ID' value="+**ID**+"/>"); 
       out.print("</td>"); 
       out.print("<td>"); 
       out.print(**ID**); 
       out.print("</td>"); 
       for (int i = 0; i < **3**; i++) { 
        out.print("<td>"); 
        out.print(it.next()); 
        out.print("</td>"); 
       } 
      out.print("</tr>"); 

      } 
     %> 
    </table> 
</form> 
</body> 
</html> 
+0

你的Java代碼是好的,但另一方面,你的jsp代碼看起來像uuugh ...你可以使用類似'ajax'的東西來做這些操作....我希望這是一個學生應用程序,而不是生產準備好的應用程序......我可以幫助你,現在我需要你在你的Delete按鈕上點擊右鍵並選擇Inspect element,然後在Dom Explorer中複製突出顯示的html並添加到你的問題... – Hackerman

回答

1

只需創建一個新的Servlet(刪除的Servlet),並通過該ID的URL和新的jsp頁面處理它...您可以更改您的代碼likethis:

指數JSP:

<a href="DeleteServlet?id=<%=Integer.toString(person.getID())%>">Delete</a> 

刪除的Servlet:更新

@WebServlet(name = "DeleteServlet", urlPatterns = {"/DeleteServlet"}) 
public class DeleteServlet extends HttpServlet { 

/** 
    * 
    */ 
    private static final long serialVersionUID = 1L; 
private static final Logger LOGGER = Logger.getLogger(CurdOperationsImpl.class.getName()); 

    @Override 
    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
      throws ServletException, IOException { 
     response.setContentType("text/html;charset=UTF-8"); 
     PrintWriter out = response.getWriter(); 
     String ID = request.getParameter("id"); 


      int id = Integer.parseInt(PersonId); 

      deleteClient(id); // add your own code 

      out.println("<h2 style='color: green'>Person Deleted Sucessfully.</h2>"); 
      response.sendRedirect("index.jsp"); 
     }else { 

     } 
    } 

獎勵:獲得這個我難看的Servlet,JSP MySQL的項目是準備使用Github link 我希望這可以幫助您。

+0

你好 如何創建DeleteServlet?在網頁 - > index.jsp? – Lobi

+0

不在index.JSP中創建單獨的servlet類。 –

+0

在源碼包文件夾中?新建 - > Java包:servlet?並在servlet包中:new - > Servlet? – Lobi

相關問題