我正在使用servlet,我被困在一个点,我只想更新那些使用复选框

时间:2015-07-04 09:29:42

标签: java mysql jdbc checkbox sql-update

我正在使用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) {}
  }



    }
    }

    }

其他服务器代码更新的地方

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


    }
 }   

}

1 个答案:

答案 0 :(得分:0)

这很容易。只有选中复选框后,浏览器才会将选中的复选框作为参数发送。您的案例更复杂,因为您有一个表,因此您需要通过为复选框提供表行唯一的值来在一行中的复选框之间取消

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

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

代码的一部分,请求的进程将收到一个多值参数&#34;编辑&#34;包含所有复选框的值。伪代码:

edit: edit1, edit5, edit6

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

祝你好运!