Java 在编辑数据库的 JSP 页面中编辑表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19026773/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Edit a table in JSP page which edits the database
提问by ABV
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. 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.
我的问题很简单,我制作了一个 JSP 页面,其中有一个显示数据库内容的表格,现在我尝试的是编辑表格中的详细信息,同时编辑数据库中的值。我已经写了代码,一切看起来都不错,但是,它没有编辑数据库,怎么办??非常需要帮助并且非常感谢。提前致谢。
<%@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>
回答by Yawar
A good way to go around this problem would be
解决这个问题的一个好方法是
- Have a servlet sitting on the backend to which can interact with database.
- Whenever user clicks on the table and modifies it, you send an ajax request to this servlet.
- The servlet interacts with the database and calls the appropriate UPDATE table methods.
- 在后端有一个可以与数据库交互的 servlet。
- 每当用户单击该表并对其进行修改时,您就会向该 servlet 发送一个 ajax 请求。
- servlet 与数据库交互并调用适当的 UPDATE 表方法。
You should have a look at AJAX to get started if you want to have things done on the fly.
如果您想即时完成任务,您应该查看 AJAX 以开始使用。
回答by Ravindranath Akila
onClick is a JavaScript thing. It doesn't submit the form, hence, request.getParameter won't work.
onClick 是 JavaScript 的东西。它不提交表单,因此 request.getParameter 将不起作用。