【问题标题】:i am working with servlets and i am stuck at one point where i want to update only those rows which are selected using checkbox我正在使用 servlet,我被困在一个地方,我只想更新那些使用复选框选择的行
【发布时间】:2015-07-12 05:17:13
【问题描述】:

我正在使用 JDBC servlets(Netbeans) 和 Mysql。我只想更新用户选中复选框的少数选定行。如果在该特定行中更新了值,用户将选中该框。我正在使用文本显示数据库表中的值,可以在其中以文本格式更改它,并在编辑后选中该框。我有项目代码作为主键,它只是显示并且一旦输入就无法更改。 请帮助我。 非常感谢您的帮助。 这是我的代码:

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) {}
  }



    }
    }

    }

进行更新的其他 Servlet 代码

 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){
        }  


    }
 }   

}

【问题讨论】:

  • 到底是什么问题?
  • @Slava Imeshev 我的问题是我无法知道选中了哪一行的复选框,以便我可以编辑表中的特定行,如第二个程序所示,我检查了所选行数的输出,但是我不知道检查了哪一行..我想知道检查了哪一行??
  • 你能帮我解决这个问题吗?

标签: java mysql jdbc checkbox sql-update


【解决方案1】:

这很容易。仅当复选框被选中时,浏览器才会在提交时将复选框作为参数发送。您的情况更复杂,因为您有一个表格,因此您需要通过为复选框赋予表格行唯一的值来区分行中的复选框

 out.print("<input type=\"checkbox\" name=\"edit\" value=\"edit\"" + someId + ">");

其中 someId 是记录的唯一 ID,当您知道表的结构时,您将自己弄清楚它应该是什么。主键列的值通常效果很好。

处理请求的代码部分将接收一个多值参数“edit”,该参数将包含所有复选框的值。伪代码:

edit: edit1, edit5, edit6

您所要做的就是处理这些部分。

祝你好运!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-15
    • 2012-12-06
    • 2021-09-02
    • 2021-03-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多