简体   繁体   English

在编辑数据库的 JSP 页面中编辑表

[英]Edit a table in JSP page which edits the database

my question is simple,i have made a JSP page having a table which displays the content of the database,now what m trying is to edit the details in table,which simultaneously edits the values in the database.我的问题很简单,我制作了一个 JSP 页面,其中包含一个显示数据库内容的表格,现在我尝试编辑表格中的详细信息,同时编辑数据库中的值。 I have written the code,everything looks good but ,its not editing the database,what to do ??我已经编写了代码,一切看起来都不错,但是,它不是在编辑数据库,该怎么办?? Help is seriously needed and is appreciated a lot.非常需要帮助,非常感谢。 Thanks in advance.提前致谢。

<%@page import="java.sql.DriverManager"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@page import="java.sql.*" %>    

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

</body>
<form method="post">

<table border="7">
<tr>
<td>ID</td>
<td>NAME</td>
<td>SKILL</td>
<td colspan="2" align="center">ACTION</td>
</tr>


<%
try
{
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost/test";
String username="root";
String password="root";
String query="select * from jsp1";

Connection conn=DriverManager.getConnection(url,username,password);
Statement stmt=conn.createStatement();


ResultSet rs=stmt.executeQuery(query);
while(rs.next())
{

%>
    <tr>
    <td><%=rs.getInt("ID") %></td>
    <td><input type="text" value="<%=rs.getString("NAME") %>"></td>
    <td><input type="text" value="<%=rs.getString("SKILL") %>"></td>
    <td><input type="button" name="UPDATE" value="UPDATE" onclick="
    <% 
    String qmod="update jsp1 set NAME=?,SKILL=? where ID=? ";
    PreparedStatement pstmt=conn.prepareStatement(qmod);
    String one=request.getParameter("NAME");
    String two=request.getParameter("SKILL");
    String three=request.getParameter("ID");
    pstmt.setString(1,one);
    pstmt.setString(2,two);
    pstmt.setString(3,three);
    pstmt.executeUpdate(); 
    %>"></td>
    <td> <input type="button" name="DELETE" value="DELETE"></td>
    </tr>
        <%

}
%>
    </table>
    <%
    rs.close();
    stmt.close();
    conn.close();
    }
    catch(Exception e)
    {
        e.printStackTrace();
        }






%>

</form>
</html>

A good way to go around this problem would be解决这个问题的一个好方法是

  1. Have a servlet sitting on the backend to which can interact with database.在后端有一个 servlet,可以与数据库交互。
  2. Whenever user clicks on the table and modifies it, you send an ajax request to this servlet.每当用户单击表并对其进行修改时,您都会向该 servlet 发送一个 ajax 请求。
  3. The servlet interacts with the database and calls the appropriate UPDATE table methods. servlet 与数据库交互并调用适当的 UPDATE 表方法。

You should have a look at AJAX to get started if you want to have things done on the fly.如果您想即时完成工作,您应该查看 AJAX 以开始使用。

onClick is a JavaScript thing. onClick 是一个 JavaScript 的东西。 It doesn't submit the form, hence, request.getParameter won't work.它不提交表单,因此 request.getParameter 将不起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM