サーブレット jsp で送信ボタンをクリックした後、データベースを更新できません。
index.jsp
<%@page import="java.sql.*"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/xigmapro","root","");
Statement st=con.createStatement();
ResultSet rs1=st.executeQuery("select * from user_information");
%>
<table border="1">
<tr>
<td width="120">User Name</td><td width="120">User Address</td>
<td width="120">User City</td><td width="120">User State</td>
<td width="120">User Mail</td><td width="120">User Phone</td>
<td width="120">User Post</td><td width="120">User Country</td>
<td width="120">Action</td>
</tr>
<%
while(rs1.next())
{
String uid=rs1.getString("client_id");
out.println(uid);
%>
<tr><td width="120"><% out.print(rs1.getString("client_name"));%></td><td
width="120"><% out.print(rs1.getString("client_address")); %></td><td width="120">
<% out.print(rs1.getString("client_city")); %></td>
<td width="120"><% out.print(rs1.getString("client_state")); %></td><td
width="120"><% out.print(rs1.getString("client_mail")); %></td>
<td width="120"><% out.print(rs1.getString("client_phone")); %></td><td
width="120"><% out.print(rs1.getString("client_post")); %></td><td width="120"><%
out.print(rs1.getString("client_country")); %></td>
<td width="190"><form action="NewServlet" method="post">
<input type="hidden" name="uid" value="<%=uid %>"/>
<input type="submit" name="send" value="Update">
</form>||<form action="NewServlet" method="post">
<input type="hidden" name="uid" value="<%=uid %>"/>
<input type="submit" name="send" value="Delete">
</form></td>
</tr>
<%
}%>
</table>
<%
}catch(Exception e)
{
out.println(e);
}
%>
</body>
</html>
NewSevlet.java
import java.awt.Choice;
import java.lang.*;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;
import java.sql.DriverManager;
import java.sql.Connection;
import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpSession;
import org.apache.catalina.Session;
public class NewServlet extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
} catch (Exception e) {
out.println(e);
}
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String Uid = request.getParameter("uid");
String choice = request.getParameter("send");
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/xigmapro",
"root", "");
Statement st = con.createStatement();
if (choice.equals("Add")) {
String uid = request.getParameter("uid");
out.println(uid);
Statement st1 = con.createStatement();
st1.executeUpdate("UPDATE user_information SET" + "`client_name`='" + request.getParameter("user_name") + "',`client_address`='" + request.getParameter("user_add") + "',`client_city`='" + request.getParameter("user_city") + "'WHERE `client_id`=uid");
//response.sendRedirect("index.jsp");
out.println("UPDATE user_information SET " + "`client_name`='" + request.getParameter("user_name") + "',`client_address`='" + request.getParameter("user_add") + "',`client_city`='" + request.getParameter("user_city") + "' WHERE `client_id`='uid'");
}
if (choice.equals("Delete")) {
st.executeUpdate("delete from user_information where `client_id`='" + Uid + "'");
out.println("user deleted from database successfully");
//RequestDispatcher rd=request.getRequestDispatcher("next.jsp");
//rd.include(request, response);
}
if (choice.equals("Update")) {
PreparedStatement ps = con.prepareStatement("SELECT *FROM xigmapro.user_information WHERE client_id = ?");
ps.setString(1, Uid);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
String UID = rs.getString("client_id");
out.println(UID);
out.println("<form action='' method='post'>");
out.println("<input type='hidden' name='uid' value = '" + rs.getString("client_id") + "' / >");
out.println("<table border='1' width='280px' height='120px'>");
out.println("<tr width='280px'><td width='180'>User Name</td><td width='120'>User Address</td > <td width = '180'>User City</td > <td width = '180'>User State</td >" + "<td width='120'>User Mail</td><td width='180'>User Phone</td><td width='120'>User Country</td > < / tr >");
out.println("<tr><td>");
out.print("<input type='text' name='user_name' value = '" + rs.getString("client_name") + "' / >" + " < /td>");
out.print("<td>" + "<input type='text' name='user_add' value = '" + rs.getString("client_address") + "' / >" + " < /td>");
out.print("<td>" + "<input type='text' name='user_city' value = '" + rs.getString("client_city") + "' / >" + " < /td>");
out.print("<td>" + "<input type='text' name='user_state' value = '" + rs.getString("client_state") + "' / >" + " < /td>");
out.print("<td>" + "<input type='text' name='user_mail' value = '" + rs.getString("client_mail") + "' / >" + " < /td>");
out.print("<td>" + "<input type='text' name='user_phone' value = '" + rs.getString("client_phone") + "' / >" + " < /td>");
out.print("<td>" + "<input type='text' name='user_contry' value = '" + rs.getString("client_country") + "' / >" + " < /td>");
out.print("</td></tr>");
out.println("</td></tr></table>");
out.println("<input type='submit' name='send' value='Add'>");
out.println("</form>");
}
}
} catch (Exception e) {
}
}
@Override
public String getServletInfo() {
return "Short description";
}
}
データベースの作成に成功し、データベースへの接続も成功しました。しかし、NewSevlet.java
ページの「追加」ボタンをクリックすると、テーブルを更新できません。[追加] 送信ボタンをクリックすると、送信した特定のユーザー ID が非表示フィールドとして表示されますが、テーブルを更新できません。また、私のクエリはOKです。Index.jsp
は最初のページで、NewServlet.java
は 2 ページ目です。