gpt4 book ai didi

java - 使用servlet通过html页面更新数据库记录

转载 作者:行者123 更新时间:2023-12-02 05:40:40 24 4
gpt4 key购买 nike

我的请求将更新 else if 条件中的查询,但数据库中的记录不会更新帮助我摆脱这种困境单击更新按钮后,我没有收到任何错误数据库记录未插入数据库

// imports ...

public class RequestData extends HttpServlet {
public void service(HttpServletRequest request, HttpServletResponse response) throws IOException {
PrintWriter out = response.getWriter();
response.setContentType("text/html");
out.print("<html>");
out.print("<title>DataRetreving</title>");
out.print("<body>");
out.print("<table border=5 width=50% height=50% bgcolor=magenta align=center>");
out.print("<tr>");
out.print("<th><b>id</b></th>");
out.print("<th><b>name</b></th>");
out.print("<th><b>address</b></th>");
out.print("<th><b>action</b></th>");
out.print("<th><b>actionupdate</b></th>");
out.print("</tr>");
Driver driver = new OracleDriver();
try {
DriverManager.registerDriver(driver);
Connection connection = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521/XE", "system",
"india124");
Statement pst = connection.createStatement();
if(request.getParameter("action")!=null && "delete".equals(request.getParameter("action")))
{
int id=Integer.parseInt(request.getParameter("id"));
pst.executeUpdate("delete from employee where id="+id);
}
else if(request.getParameter("id") != null
&& request.getParameter("name") != null
&& request.getParameter("address") != null
&& request.getParameter("update") != null
&& !"update".equals(request.getParameter("actionupdate")))
{
int id = Integer.parseInt(request.getParameter("id"));
String name = request.getParameter("name");
String address = request.getParameter("address");
pst.executeQuery("insert into employee(id,name,address)"+"values("+id+",'"+name+"','"+address+"')");
}
else if(request.getParameter("actionupdate")!=null
&& "update".equals(request.getParameter("actionupdate"))) {
int id = Integer.parseInt(request.getParameter("id"));
String name = request.getParameter("name");
String address =request.getParameter("address");
pst.executeUpdate("update employee set address='"+address+"',name='"+name+"'where id="+id+"");
}

ResultSet rst=pst.executeQuery("select *from employee");
while (rst.next())
{
out.print("<tr>");
out.print("<td align=center bgcolor=yellow><b> " + rst.getInt("id") + "</b></td>");
out.print("<td align=center><b>" + rst.getString("name") + "</b></td>");
out.print("<td align=center><b>" + rst.getString("address") + "</b></td>");
out.print("<form align=center action=\"./requestdata\">");
out.print("<td align=center bgcolor=yellow><a href=\"http://localhost:8082/RequestDataDemo/requestdata?id="
+rst.getInt("id")+"&action=delete\">DELETE</a></td>");
out.print("<td align=center bgcolor=yellow><a href=\"http://localhost:8082/RequestDataDemo/requestdata?id="
+rst.getInt("id")+"&name="+rst.getString("name")+"&address="+rst.getString("address")+"&actionupdate=update\">UPDATE</a></td>");
out.print("</tr>");
}
out.print("</table>");
out.print("<br><br>");

if(request.getParameter("actionupdate")!=null && "update".equals(request.getParameter("actionupdate"))) {
out.print("<form align=center action=\"./requestdata\">");
out.print("enter id :<input type=\" text\" name = \" id \" value="+request.getParameter("id")+"><br><br>");
out.print("enter name: <input type=\"text\" name = \"name\" value="+request.getParameter("name")+"><br><br>");
out.print("enter address:<input type=\"text\" name = \"address \" value="+request.getParameter("address")+"><br><br>");
out.print(" <input type=\"submit\" value =\"update\"><br><br>");
out.print("</form>");
}
else
{
out.print("<form align=center action=\"./requestdata\">");
out.print("eneter id :<input type=" + "text" + " name = " + "id><br><br>");
out.print("eneter name: <input type=" + "text" + " name = " + "name><br><br>");
out.print("eneter address:<input type=" + "text" + " name = " + "address><br><br>");
out.print(" <input type=" + "submit" + " value = " + "submit><br><br>");
out.print(" <input type=" + "reset><br><br>");
/* out.print("<input type='submit' value='delete'>"); */
out.print("</form>");
}
out.print("</body>");
out.print("</html>");
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
out.close();
}
}

我的请求将要更新,但数据库中的记录没有更新帮助我摆脱这种困境单击更新按钮后,我没有收到任何错误数据库记录未插入数据库

最佳答案

我同意您应该重构此代码的所有评论。但对于第一次尝试,我想看到它起作用很重要。

我认为你在这里有一个逻辑问题:当你插入一条新记录时,你到底发送了什么?我找不到那部分。

我猜你希望插入 block 运行,所以这个条件:

else if(request.getParameter("id") != null&&request.getParameter("name")!=null&&request.getParameter("address")!=null &&request.getParameter("update")!=null &&!"update".equals(request.getParameter("actionupdate") ))

说实话。但是,如果您查看该行的末尾,您希望存在“update”参数,并且“actionupdate”与“update”不同。但您没有在代码中设置这些。所以这个条件总是为假,因此插入永远不会被调用。

当您按下提交按钮时,参数 Submit=""会发送到服务器。这样您就可以知道单击了哪个按钮。因此,可以添加一个带有 value="insert"的提交按钮,并测试该按钮是否存在以进入 INSERT block 。

再次,尝试通过重新访问所有条件和参数来完成这项工作,并查看它们何时/是否设置以及何时正确或错误。之后绝对要花一些时间重构和练习更好的技术。

关于java - 使用servlet通过html页面更新数据库记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56142782/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com