如何在JSP中的derby中插入和删除数据库中的值? [3] [英] How can I insert and delete value in a database in derby in JSP? [3]
本文介绍了如何在JSP中的derby中插入和删除数据库中的值? [3]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
The history: History I. History II.
因此问题仍然存在.如果我选择某人并按Delete键,则不是要从数据库中删除,否则要创建一个新的空记录.
So the problem is still here. If I select somebody and press the delete button, not to delete from database else a new empty record created.
client.java:
The 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:
The 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> </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>
推荐答案
只需创建一个新的Servlet(删除Servlet)并在url中传递ID并在新的jsp页面中对其进行处理即可...您可以像这样更改代码:
Just create a new Servlet (Delete Servlet) and pass the ID in url and handle it in new jsp page ...You can change your code 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链接 希望对您有帮助.
Bonus : get this my ugly Servlet-JSP-Mysql project is ready to use Github link I hope this help you.
这篇关于如何在JSP中的derby中插入和删除数据库中的值? [3]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文