【问题标题】:How to get selected radio button's value inside of a servlet and store it inside DB?如何在 servlet 中获取选定单选按钮的值并将其存储在 DB 中?
【发布时间】:2019-09-07 05:36:22
【问题描述】:

我正在开发一个投票应用程序,人们可以在其中为特定选项投票并提交。现在,我希望用户在 servlet 中选择的选项并将其作为投票存储在数据库中。当收到新的投票时,该计数应始终增加 1。但是,它没有给我想要的输出。每次当我选择一个选项并单击提交时,1 都会像投票一样插入到数据库的相应列中。我已将选项命名为 db 中的列名。我希望每一个新投票都是对其先前价值的补充

下面是 Voting.jsp 页面:-

<div class="card" style="width: 60rem; margin-left: 45px;">
  <div class="card-body">
  Click on one of the following options to caste your vote. Then click submit.

  </div>
  </div>


<div class="card" style="width: 60rem; margin-left: 45px;">
  <div class="card-body">
  <form action="SuccessServlet" method="post">
    <label class="container">Team_1
  <input type="radio" name="radio" value="team1">
  <span class="checkmark"></span>
</label>
<label class="container">Team_2
  <input type="radio" name="radio" value="team2">
  <span class="checkmark"></span>
</label>
<label class="container">Team_3
  <input type="radio" name="radio" value="team3">
  <span class="checkmark"></span>
</label>
<label class="container">Team_4
  <input type="radio" name="radio" value="team4">
  <span class="checkmark"></span>
</label>
<button type="submit">Submit</button>
</form>
  </div>
</div>

以下是我的 Servlet 类:-

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {



        ud.setFirst_name(request.getParameter("f1"));



        String answer = request.getParameter("radio");

        if("team1".equals(answer)) {

            System.out.println("Hello");

            writeData1();

            }

    else if ("team2".equals(answer)) {

        writeData2();

    }

    else if ("team3".equals(answer)) {

        writeData3();
    }

    else if ("team4".equals(answer)) {

        writeData4();
    }

    else {
        response.sendRedirect("Success.jsp");
    }
    response.sendRedirect("UserPage.jsp");

}



    }


      public void writeData1() {

      try {
        Class.forName("com.mysql.cj.jdbc.Driver");


      Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/my_db?autoReconnect=true&useSSL=FALSE", "root", "root");


      String query = "insert into Total_Votes(Team1) values(?)";

      PreparedStatement ps = con.prepareStatement(query);

      ps.setInt(1, 1);

      ps.executeUpdate();


      } catch (Exception e) {
            // TODO Auto-generated catch block
            System.out.println(e.getMessage());
        }





  }





public void writeData2() {

      try {
        Class.forName("com.mysql.cj.jdbc.Driver");


      Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/my_db?autoReconnect=true&useSSL=FALSE", "root", "root");


      String query = "insert into Total_Votes(Team2) values(?)";

      PreparedStatement ps = con.prepareStatement(query);

      ps.setInt(1, 1);

      ps.executeUpdate();


      } catch (Exception e) {
            // TODO Auto-generated catch block
            System.out.println(e.getMessage());
        }





  }





public void writeData3() {

      try {
        Class.forName("com.mysql.cj.jdbc.Driver");


      Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/my_db?autoReconnect=true&useSSL=FALSE", "root", "root");


      String query = "insert into Total_Votes(Team3) values(?)";

      PreparedStatement ps = con.prepareStatement(query);

      ps.setInt(1, 1);

      ps.executeUpdate();


      } catch (Exception e) {
            // TODO Auto-generated catch block
            System.out.println(e.getMessage());
        }





  }




public void writeData4() {

      try {
        Class.forName("com.mysql.cj.jdbc.Driver");


      Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/my_db?autoReconnect=true&useSSL=FALSE", "root", "root");


      String query = "insert into Total_Votes(Team4) values(?)";

      PreparedStatement ps = con.prepareStatement(query);

      ps.setInt(1, 1);

      ps.executeUpdate();


      } catch (Exception e) {
            // TODO Auto-generated catch block
            System.out.println(e.getMessage());
        }





  }

【问题讨论】:

  • 它给你什么输出?你收到什么错误?数据库中的最终值是什么?到目前为止,您尝试过什么?
  • 每当我选择一个选项并单击提交时,1 就会像投票一样插入到数据库的相应列中。我已将选项命名为 db 中的列名。我希望每一个新投票都是对其先前价值的补充。我已经提到了上面的代码,这就是我到目前为止所尝试的
  • @ShashankkShekarChaturvedi 提供更多详细信息作为对您问题的编辑,而不是作为评论。

标签: java mysql jsp servlets jdbc


【解决方案1】:

您的代码始终将 1 作为指定团队的总票数。如果您在需要知道总数时使用 SQL 中的SUM() 函数来汇总团队的投票数,那很好。

还有另外两种处理方法:

  1. 每个团队的数据库中只有一条记录。每次投票时,读取当前值(如果找不到当前值,则假定为 0),递增它,然后用新值更新记录。

  2. 继续为每次投票插入一条记录。每次投票时,读取当前的 MAX() 值(如果找不到当前值,则假定为 0),将其递增并INSERT使用新值创建另一条记录。

【讨论】:

    【解决方案2】:

    假设team1 单选按钮被选中,现在您需要使用select 查询来获取team1 先前投票的值,如下所示:

       try {
                Class.forName("com.mysql.cj.jdbc.Driver");
              Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/my_db?autoReconnect=true&useSSL=FALSE", "root", "root");
        String sql="select votes from Total_Votes where team=?";
                         int votes=0;
                        PreparedStatement ps = con.prepareStatement(sql);
                       ps.setString(1,"team1");//passing team1 in query
                       rs = ps.executeQuery();
                        while(rs.next())
                       {
              votes=rs.getInt("yourcoulmnname");//getting value of coulmn where votes are stored for team1
                      }
                       }catch(Exception e)
                       {
                       System.out.println(e.getMessage());
                       }
    

    现在,使用update 查询更新投票,如下所示:

    String sql1 = "UPDATE `Total_Votes` SET `yourcoulumnname`=? WHERE `team`=?";
                 PreparedStatement ps = con.prepareStatement(sql1);
                int v=votes+1;//votes got from previous select query add by 1 
                 ps.setInt(1,v);
                 ps.setString(2,"team1");
    
                 int i=0;
                  i = ps.executeUpdate();
    
             if(i>0){
                 System.out.println("updated");
               }
    

    希望对您有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-03
      • 1970-01-01
      • 1970-01-01
      • 2012-07-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-10
      相关资源
      最近更新 更多