0

您好,制作了一个简单的投票系统,显示投票数和喜欢的人数。我正在使用 html 页面创建一个表单,其中列出了您可以使用单选按钮投票的食物。这是页面:

<HTML>
    <HEAD><TITLE></TITLE></HEAD>


    <FORM ACTION="NewServlet" method="post">
        <br>

        <b> Select your favorite food:</B>

        <br>
            <input type="radio" name="choice" value="Ice Cream">Ice Cream
            <br>

            <input type="radio" name="choice" value ="Hamburger">Hamburger
              <br>

              <input type="radio" name="choice" value ="Pizza">Pizza
                <br>
        <br />
        <br />
            <INPUT TYPE="submit" value="Query" > 
            <INPUT TYPE="reset" value="Reset"> 
    </FORM>
</BODY>
</HTML>

这是我创建的 servlet 页面,它将连接到数据库并更改列,这样每当有人在那里投票时,like 就会增加一。我只是不知道该怎么做。这是我到目前为止所拥有的:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 *
 * @author Danny
 */
@WebServlet(urlPatterns = {"/NewServlet"})
public class NewServlet extends HttpServlet {



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

        String driver = "com.mysql.jdbc.Driver";

        try {
            Class.forName(driver).newInstance();
            Connection connection;
            String choice[] = request.getParameterValues("choice");

         connection = (Connection) DriverManager.getConnection("jdbc:mysql://localhost", "root", "symphonia23");
            Statement s = connection.createStatement();
               s.executeQuery("use food");

            if (choice.length != 0) {
                for (int i = 0; i <= choice.length; i++) {
                    s = connection.createStatement();
                    s.executeQuery("use food");

                    s.executeUpdate("UPDATE Food SET Liked = Liked +1 WHERE Food = '"
                            + choice[i] + "';");
                }
            }
            s = connection.createStatement();
            ResultSet rs = s.executeQuery("SELECT * FROM favoritefood");

            response.setContentType("text/html");
            PrintWriter out = response.getWriter();
            out.println("<html>");
            out.println("<body>");
            out.println("<tabe>");
            while (rs.next()) {
                out.println("<tr>");
                out.println("<td>");
                out.println(rs.getString("Food"));
                out.println("</td>");
                out.println("<td>");
                out.println(rs.getInt("Liked"));
                out.println("<br>");
                out.println("</td>");
                out.println("</tr>");
            }
            out.println("</tabe>");
            out.println("</body>");
            out.println("</html>");
            out.close();

        } catch (SQLException e) {
            e.printStackTrace();
        } catch (InstantiationException | IllegalAccessException
                | ClassNotFoundException e1) {
            e1.printStackTrace();
        }

    }

}

我的桌子看起来像这样

http://i.imgur.com/2fyeCe9.png )

我希望输出在网页上看起来像这样:

http://i.imgur.com/8HjR4sv.png

4

0 回答 0