i am using JDBC servlets(Netbeans)and Mysql. I want to update only few selected rows whose check box is checked by user. The user will check the box if value is updated in that particular row. I am displaying the values from database table using text where it can be changed in text format and check the box once edited. I have project Code as primary key which is just displayed and cannot be changed once entered. Please help me with the same. Thank you very much in advance for your help. Here is my code:
import java.io.*;
import javax.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class newna extends HttpServlet
{
@Override
public void service(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
response.setContentType("text/html");
try (PrintWriter out = response.getWriter())
{
out.print("<html>");
out.print("<head><title>Servlet JDBC</title></head>");
out.print("<body>");
out.print("<h1>Servlet </h1>");
out.print("</body></html>");
// connecting to database
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection
("jdbc:mysql://localhost:3306/database",
"root", "password");
stmt = con.createStatement();
rs = stmt.executeQuery("SELECT *FROM admin_pr");
// displaying records
out.print(" <form action=\"admin_pr_up\" method=\"post\">");
out.print("<table border=1>");
out.print("<tr>");
out.print("<th>Project Code </th>");
out.print("<th> Part Description </th>");
out.print("<th> Project Start Date </th>");
out.print("<th> Project End Date </th>");
out.print("<th> Design Start Date</th>");
out.print("<th> Design End Date </th>");
out.print("<th> Edited </th>");
out.print("</tr>");
while(rs.next()){
out.print("<tr>");
out.print("<td>");
out.print(rs.getObject(1).toString());
//pr_code which is not
//changed and is primary key according to which i'll update.
out.print("</td>");
out.print("<td>");
out.print(" <input type=\"text\"name=\"prdesc\"value=\""+rs.getObject(2).toString()+"\">");
out.print("</td>");
out.print("<td>");
out.print(" <input type=\"text\" name=\"pr_str_date\" value=\""+rs.getObject(3).toString()+"\">");
out.print("</td>");
out.print("<td>");
out.print(" <input type=\"text\" name=\"pr_end_date\"value=\""+rs.getObject(4).toString()+"\">");
out.print("</td>");
out.print("<td>");
out.print(" <input type=\"text\" name=\"des_str_date\" value=\""+rs.getObject(5).toString()+"\">");
out.print("</td>");
out.print("<td>");
out.print(" <input type=\"text\" name=\"des_end_date\" value=\""+rs.getObject(6).toString()+"\">");
out.print("</td>");
out.print("<td>");
out.print("<input type=\"checkbox\" name=\"edit\" value=\"edit\">");
out.print("</td>");
out.print("</tr>");
}
out.print("</table>");
// out.print(" <input type=\"text\" name=\"pr_code\"\">");
out.print("<input type=\"submit\" value=\"edit\"><br>");
out.print("</form>");
} catch (SQLException e)
{
throw new ServletException("Servlet Could not display records.", e);
} catch (ClassNotFoundException e)
{
throw new ServletException("JDBC Driver not found.", e);
} finally
{
try {
if(rs != null)
{
rs.close();
rs = null;
}
if(stmt != null)
{
stmt.close();
stmt = null;
}
if(con != null)
{
con.close();
con = null;
}
} catch (SQLException e) {}
}
}
}
}
THE OTHER SERVLET CODE WHERE UPDATING TAKES PLACE
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.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.servlet.RequestDispatcher;
public class admin_pr_up extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException{
Connection connection = null;
Statement istmt = null;
ResultSet rs = null;
//ResultSet rs1 = null;
try{
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/database", "root", "password");
istmt = connection.createStatement();
rs=istmt.executeQuery("select * from admin_pr ");
PrintWriter out = response.getWriter();
String[] cbox = request.getParameterValues("cbox");
for(int i=1;i<=cbox.length;i++)
{
if (cbox!= null)
{
out.print("CHECKED");
}
}
}catch(ClassNotFoundException | SQLException e){
}finally{
try{
istmt.close();
}catch(Exception e){
}
}
}
}
Aucun commentaire:
Enregistrer un commentaire