在Mysql Servlet中将值递增1

时间:2013-11-02 19:18:24

标签: java html mysql database servlets

嗨,制作一个简单的投票系统,用它所拥有的数量显示投票。我正在使用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页面,它将连接到数据库并更改列,以便每当有人登顶时,类似会增加1。我只是无法想象如何做到这一点。这就是我到目前为止所做的:

/*
 * 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

0 个答案:

没有答案