我需要你的一点帮助,我已经重新编辑了我的代码并再次发布在这里,我想通过单击 EDIT 按钮来编辑数据。任何人都可以帮我一点,如何链接该编辑功能?
索引.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>
数据插入表.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();
}
}
数据更新表.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());
}
}
}