我需要你的帮助,我已经重新编写代码并再次发布在此处,我想根据点击编辑按钮编辑数据。 任何人都可以帮我一点点如何链接该编辑功能?
的index.jsp
<html>
<head>
<title>Data insertion form</title>
</head>
<body>
<%
String action="/SimpleServlet/DataInsertTable";
String method="get";
if(request.getAttribute("EDIT")!=null) {
action="/SimpleServlet/DataUpdateTable";
method="post";
}
%>
<form method= "<%=method%>" action="<%=action%>">
<h1><center>Enter the required information</center></h1>
<table>
<tr>
<td>Name</td>
<td> :- </td>
<%if(request.getAttribute("NAME")!=null){ %>
<td><input type="text" name="name" value="<%=request.getAttribute("NAME")%>"></input>
<%}else{ %>
<td><input type="text" name="name"></input>
<%} %>
</td>
</tr>
<tr>
<td>Roll Number</td>
<td> :- </td>
<%if(request.getAttribute("ROLL")!=null){ %>
<td><input type="text" name="roll" value="<%=request.getAttribute("ROLL")%>"></input>
<%}else{ %>
<td><input type="text" name="roll"></input>
<%} %>
</td>
</tr>
<tr>
<td>Class </td>
<td> :- </td>
<%if(request.getAttribute("CLAS")!=null){ %>
<td><input type="text" name="clas" value="<%=request.getAttribute("CLAS")%>"></input>
<%}else{ %>
<td><input type="text" name="clas"></input>
<%} %>
</td>
</tr>
<tr>
<td>Mobile Number </td>
<td> :- </td>
<%if(request.getAttribute("MONO")!=null){ %>
<td><input type="text" name="mono" value="<%=request.getAttribute("MONO")%>"></input>
<%}else{ %>
<td><input type="text" name="mono"></input>
<%} %>
</td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Submit" width="100%"></input></td>
</tr>
</table>
</form>
</body>
</html>
DataInsertTable.java
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class DataInsertTable extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws IOException, ServletException {
res.setContentType("text/html");
PrintWriter out = res.getWriter();
PrintWriter pwinsert = res.getWriter();
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
Statement st = null;
out.println("<html>");
out.println("<head>");
out.println("<title>User Data</title>");
out.println("</head>");
out.println("<body>");
out.println("<center><u><h1>User Data</h1></u>");
out.println("<form name='form' >");
out.println("<table border="+2+ "> ");
out.println("<tr>");
out.println("<td> Select </td>");
out.println("<td> Name </td>");
out.println("<td> Roll No. </td>");
out.println("<td> Class </td>");
out.println("<td> Mobile Number </td>");
out.println("<td> Edit </td>");
out.println("</tr>");
String nm = req.getParameter("name");
String roll = req.getParameter("roll");
String clas = req.getParameter("clas");
String mono = req.getParameter("mono");
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
}
catch(ClassNotFoundException ex) {
System.out.println("driver not loaded");
System.exit(0);
}
String URL = "jdbc:oracle:thin:@192.168.106.87:1521:ORA11G";
String Username = "pratik";
String Password = "pratik";
try {
con = DriverManager.getConnection(URL,Username,Password);
// if(req.getParameter("nm")==null && req.getParameter("roll")==null && req.getParameter("clas")==null && req.getParameter("mono")==null) {
// Insert
if(req.getParameter("choise")==null) {
ps = con.prepareStatement("INSERT INTO student (name, rollno, class, mobileno) VALUES (?, ?, ?, ? )");
ps.setString(1,nm);
ps.setString(2,roll);
ps.setString(3,clas);
ps.setString(4,mono);
int i = ps.executeUpdate();
pwinsert.println(i);
if(i!=0) {
pwinsert.println("Your data has been stored in the database");
}
else {
pwinsert.println("Your data could not be stored in the database");
}
}
}
catch(Exception e) {
pwinsert.println(e.getMessage());
}
// Delete
String idr=null;
if(req.getParameter("choise")!=null) {
nm = req.getParameter("choise");
idr = "DELETE from student WHERE name ='"+nm+"'";
try {
st=con.createStatement();
rs = st.executeQuery(idr);
}
catch (Exception e) {
e.getMessage();
System.out.println("Error " +e);
}
System.out.println("Data deleted...");
}
// res.sendRedirect("DataInsertTable.java");
idr = "SELECT * FROM student WHERE name IS NOT NULL ORDER BY name";
try {
st=con.createStatement();
rs = st.executeQuery(idr);
}
catch (Exception e) {
e.getMessage();
System.out.println("Error " +e);
}
try {
while (rs.next()) {
out.println("<tr>");
out.println("<td>" + "<input type=\"radio\" name=\"choise\" value=\"" + rs.getString(1) + "\" /> </br>" + "</td>");
out.println("<td>" + rs.getString(1) + "</td>" + "\t <td>" + rs.getInt(2) + "</td>" + "\t <td>" + rs.getString(3) + "</td>" + "\t <td>" + rs.getString(4));
out.println("<td>" + "<a href='"+req.getContextPath()+"/DataUpdate?mode=EDIT&name="+rs.getString(1)+"&roll="+rs.getInt(2)+"&clas="+rs.getString(3) +"&mono="+rs.getString(4)+"'>Edit</a> </br>" + "</td>");
out.println("</tr>");
}
}
catch (Exception e) {
e.getMessage();
System.out.println("Error" +e);
}
out.println("");
out.println("<tr>");
out.println("<td> </td>");
out.println("<td>" + "<form>" + "<input type=\"button\" name = \"nw\" value=\"Add More Users\" onClick=\"window.location.href='"+ req.getContextPath()+"/index.jsp'\">" + "<form>" + "<td>");
out.println("<td>" + "<form>" + "<input type=\"button\" name = \"edit\" value=\"Edit userdata\" onClick=\"window.location.href='"+ req.getContextPath()+"/DataUpdateTable'\">" + "<form>" + "<td>");
// out.println("<td>" + "<input type=\"submit\" value = \"Edit\">" + "<a href='/SimpleServlet/'></a> </br>" + "</input>" + "</td>");
out.println("<td>" + "<input type=\"submit\" name=\"del\" value=\"Delete\" />" + "</td>");
out.println("<td>" + "<input type=\"submit\" value=\"Update\" />" + "</td>");
out.println("</tr>");
try {
rs.close();
}
catch (Exception e) {
e.getMessage();
System.out.println("Error" +e);
}
try {
st.close();
}
catch (Exception e) {
e.getMessage();
System.out.println("Error" +e);
}
out.println("</table>");
out.println("</form>");
out.println("</center>");
out.println("</body>");
out.println("</html>");
out.close();
}
}
DataUpdateTable.java
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class DataUpdateTable extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws IOException, ServletException {
res.setContentType("text/html");
PrintWriter out = res.getWriter();
PrintWriter pwinsert = res.getWriter();
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
Statement st = null;
out.println("<html>");
out.println("<head>");
out.println("<title>User Data</title>");
out.println("</head>");
out.println("<body>");
out.println("<center><u><h1>User Data</h1></u>");
out.println("<form name='form' >");
// int a = 2;
out.println("<table border="+2+ "> ");
out.println("<tr>");
out.println("<td> Name </td>");
out.println("<td> Roll No. </td>");
out.println("<td> Class </td>");
out.println("<td> Mobile Number </td>");
out.println("</tr>");
String nm = req.getParameter("name");
String roll = req.getParameter("roll");
String clas = req.getParameter("clas");
String mono = req.getParameter("mono");
req.setAttribute("NAME", nm);
req.setAttribute("ROLL", roll);
req.setAttribute("CLAS", clas);
req.setAttribute("MONO", mono);
req.setAttribute("EDIT", "Y");
req.getRequestDispatcher("/index.jsp").forward(req, res);
}
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws IOException, ServletException {
res.setContentType("text/html");
PrintWriter out = res.getWriter();
PrintWriter pwinsert = res.getWriter();
// PrintWriter pwdelete = res.getWriter();
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
Statement st = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
}
catch(ClassNotFoundException ex) {
System.out.println("driver not loaded");
System.exit(0);
}
String URL = "jdbc:oracle:thin:@192.168.106.87:1521:ORA11G";
String Username = "pratik";
String Password = "pratik";
try {
con = DriverManager.getConnection(URL,Username,Password);
String nm = req.getParameter("name");
String roll = req.getParameter("roll");
String clas = req.getParameter("clas");
String mono = req.getParameter("mono");
if(req.getParameter("choise")==null) {
ps = con.prepareStatement("update student set name=?, rollno=?, class=?, mobileno=? where rollno=?");
ps.setString(1,nm);
ps.setString(2,roll);
ps.setString(3,clas);
ps.setString(4,mono);
ps.setString(5,roll);
int i = ps.executeUpdate();
pwinsert.println(i);
if(i!=0) {
pwinsert.println("Your data has been stored in the database");
}
else {
pwinsert.println("Your data could not be stored in the database");
}
res.sendRedirect( req.getContextPath()+ "/DataInsertTable");
// res.sendRedirect("https://www.google.co.in/");
}
}
catch(Exception e) {
pwinsert.println(e.getMessage());
}
}
}
答案 0 :(得分:0)
第一次进入jsp时,您打印表单并显示您的字段没有太多麻烦(除了结构问题)。但是,在servlet中,您直接打印您的html,正如我在之前的评论中告诉您的那样,您不包含任何名称字段。 所以,而不是这样做:
out.println("<td>" + "<input type=\"radio\" name=\"choise\" value=\"choise\" /> </br>" + "</td>");
你可以这样做:
out.println("<td>" + "<input type=\"radio\" name=\"name\" value=\"choise\" /> </br>" + "</td>");
此外,没有表单标签且没有提交按钮,那么,如果您不发送数据,如何假装发送所选学生?我认为你没有想到这个,因为你真的不明白应用程序正在流动。所以简短的回答就是这样做:
out.println("<form method=\"get\" action=\"/datainsert/DataInsertTable\">"); //added line
out.println("<tr>");
while (rs.next()) {
...//you get the idea
}
out.println("</form>"); //added line
但是为了解决这类问题,你必须阅读关于java网络编程的良好实践的教程!
答案 1 :(得分:0)
在此行之后的DataInsertTable
内
out.println("</html>");
你必须添加
response.setAttribute("EDIT", "yes");
因为您正在根据EDIT参数更改目标,但您从未真正设置它。而不是这样做:
out.println("<td>" + "<input type=\"submit\" value = \"Edit\">" + "<a href='"+req.getContextPath()+"/DataUpdate'></a> " + "</input>");
你应该做
out.println("<td>" + "<input type=\"submit\" value = \"Edit\">" + "<a href='"+req.getContextPath()+"/DataUpdateTable'></a> " + "</input>");
注意:我假设您将DataUpdateTable
servlet映射到DataUpdateTable
,就像在action="/SimpleServlet/DataUpdateTable";
中一样。但我只能确定你提供的配置。
此外,我不知道你在这里假装什么,但它没有任何意义,因为你已经在形式:
out.println("<td>" + "<FORM>" + "<INPUT Type=\"BUTTON\" VALUE=\"Add More Users\" ONCLICK=\"window.location.href='"+ req.getContextPath()+"/index.jsp'\">" + "<FORM>" + "<td>");