jsp实现一个简单的投票系统

投票系统

实现结果图
jsp实现一个简单的投票系统
jsp实现一个简单的投票系统
jsp实现一个简单的投票系统
jsp实现一个简单的投票系统
(1)JDBC数据库连接工具类conn.java

package com.gx.bean;

import java.sql.*;

public class conn {
    public Connection getConn(){
        Connection conn = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3308/manage","root","yww1352465324");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e){
            e.printStackTrace();
        }
        return conn;
    }
    public PreparedStatement prepare(Connection conn,String sql){
        PreparedStatement pstmt = null;
        try{
            if (conn != null) {
                pstmt = conn.prepareStatement(sql);
            }
        }catch (SQLException e){
            e.printStackTrace();
        }
        return  pstmt;
    }
    public Statement getStatement(Connection conn){
        Statement stmt = null;
        try{
            if (conn != null) {
                stmt = conn.createStatement();
            }
        }catch (SQLException e){
            e.printStackTrace();
        }
        return stmt;
    }
    public ResultSet getResultSet(Statement stmt,String sql){
        ResultSet rs = null;
        if(stmt!=null){
            try {
                rs = stmt.executeQuery(sql);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        return rs;
    }
    public void excuteUpdate(Statement stmt,String sql){
        if(stmt!=null){
            try {
                stmt.executeUpdate(sql);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
    public void close(Connection conn){
        if(conn!=null){
            try {
                conn.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            conn=null;
        }
    }
    public void close(ResultSet rs){
        if(rs!=null){
            try {
                rs.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            rs=null;
        }
    }
    public void close(Statement stmt){
        if(stmt!=null){
            try {
                stmt.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            stmt=null;
        }
    }
}

(2)web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
</web-app>

(3)index.jsp

<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.Statement" %><%--
  Created by IntelliJ IDEA.
  User: 12526
  Date: 2021/1/15
  Time: 17:15
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>投票系统</title>
</head>
<body>
<jsp:useBean id="DB" class="com.gx.bean.conn" />
<center>
    <h2>您最喜爱的电影是</h2>
    <table border bordercolor="#0066ff" bgcolor="#CCFFFF">
        <form action="vote.jsp" method="post">
            <%
                Connection conn = DB.getConn();
                Statement stmt = DB.getStatement(conn);
                ResultSet rs = DB.getResultSet(stmt,"select * from vote");
                while(rs.next()){
                    out.println("<tr>");
                    out.println("<td><input type='radio' name='id' value='"+rs.getString("id")+"'>");
                    out.println(rs.getString("item")+"</td>");
                    out.println("</tr>");
                }
                DB.close(conn);
                DB.close(stmt);
                DB.close(rs);
                //session.setMaxInactiveInterval (-1),就是说设置的这个session永远不会失效
                session.setMaxInactiveInterval(-1);
            %>
            <tr><td align="center"><input type="submit" value="投票"></td></tr>
        </form>
        <tr><td align="center"><a href="info.jsp">查看投票</a></td></tr>
    </table>
    <p><a href="manage.jsp">投票系统维护</a></p>
</center>
</body>
</html>

(4)add.jsp

<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.Statement" %>
<%@ page import="java.sql.ResultSet" %><%--
  Created by IntelliJ IDEA.
  User: 12526
  Date: 2021/1/16
  Time: 18:36
  To change this template use File | Settings | File Templates.
--%>
<%--<%@ include file="checkadmin.jsp"%>--%>
<%@ page contentType="text/html;charset=utf-8" language="java" %>
<%@ include file="checkadmin.jsp"%>
<html>
<head>
    <title>Title</title>
</head>
<body>
<jsp:useBean id="DB" class="com.gx.bean.conn" scope="session"/>
<center>
    <h2>系统维护</h2>
    <table border bordercolor="#0099FF">
        <tr>
            <th colspan="3" bgcolor="#CCFFFF">
                删除投票项
            </th>
            <%
                Connection conn = DB.getConn();
                Statement stmt = DB.getStatement(conn);
                ResultSet rs = DB.getResultSet(stmt,"select * from vote");
                int i = 1;
                while (rs.next()){
                    out.println("<tr>");
                    out.println("<td>"+i+"</td>");
                    out.println("<td>"+rs.getString("item")+"</td>");
                    out.println("<td align='center'><a href='delete.jsp?delid="+ rs.getString("id") +" '>删除</a></td>");
                    i++;
                }
                DB.close(rs);
                DB.close(conn);
                DB.close(stmt);
            %>
        </tr>
        <tr>
            <th colspan="3" bgcolor="#CCFFFF">
                添加投票项
            </th>
        </tr>
        <tr>
            <td colspan="3">
                <form action="add.jsp" method="post">
                    内容:
                    <input type="text" name="additem" size="30">
                    <input type="submit" value="提交">
                    <input type="reset" value="重置">
                </form>
            </td>
        </tr>
        <tr>
            <td colspan="3" align="center" bgcolor="#CCFFFF">
                <a href="index.jsp">返回投票页面</a>
            </td>
        </tr>
    </table>
</center>
</body>
</html>

(5)login.jsp

<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.sql.Statement" %><%--
  Created by IntelliJ IDEA.
  User: 12526
  Date: 2021/1/16
  Time: 12:12
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=utf-8" language="java" %>
<html>
<head>
    <title>登录页</title>
</head>
<body>
<jsp:useBean id="DB" class="com.gx.bean.conn" scope="session"/>
<center>
    <form action="process.jsp" method="post">
        <table>
            <tr>
                <td width="40%">用 户 名:</td>
                <td><input type="text" name="user"></td>
            </tr>
            <tr>
                <td width="40%">密 码:</td>
                <td><input type="password" name="pw"></td>
            </tr>
            <tr>
                <td colspan="2" align="center">
                    <input type="submit" value="登录">
                    &nbsp;&nbsp;&nbsp;&nbsp;
                    <input type="reset" value="清空">
                </td>
            </tr>
        </table>
    </form>
</center>
</body>
<%
    String warning = request.getParameter("warning");
    if (warning!=null){
        warning =new String(warning.getBytes());
        out.println("<h3>"+warning+",请重新登录或<a href='index.jsp'>返回首页</a></h3>");
    }
%>
</html>

(6)vote.jsp

<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.Statement" %>
<%@ page import="java.sql.ResultSet" %><%--
  Created by IntelliJ IDEA.
  User: 12526
  Date: 2021/1/15
  Time: 17:47
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title></title>
</head>
<body>
<jsp:useBean id="DB" class="com.gx.bean.conn" />
<%
    String id = request.getParameter("id");
    Connection conn = DB.getConn();
    Statement stmt = DB.getStatement(conn);
    ResultSet rs = DB.getResultSet(stmt,"select * from vote where id ="+id);
    int num = 0;
    if(rs.next()){
        num = rs.getInt("count");
        System.out.println(num);
    }
    num++;
    System.out.println(num);
    //空格一定要书写规范,否则sql语句会失败
    String sql = "update vote set count ="+num+" where id="+id+"";
    DB.excuteUpdate(stmt,sql);
    DB.close(conn);
    DB.close(stmt);
    DB.close(rs);
%>
<jsp:forward page="info.jsp" />
</body>
</html>

(7)process.jsp

<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.sql.Statement" %><%--
  Created by IntelliJ IDEA.
  User: 12526
  Date: 2021/1/16
  Time: 12:14
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=utf-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<jsp:useBean id="DB" class="com.gx.bean.conn" scope="session" />
<%
    Connection conn = DB.getConn();
    Statement stmt = DB.getStatement(conn);
    String user = request.getParameter("user");
    String password = request.getParameter("pw");
    System.out.println(user);
    System.out.println(password);
    String sql = "select * from admin where name = '" + user + "' and password = '" + password + "'";
    ResultSet rs = DB.getResultSet(stmt,sql);
    System.out.println(rs);
    if(rs.next()){
        DB.close(rs);
        DB.close(conn);
        DB.close(stmt);
        session.setAttribute("admin","ok");
%>
<jsp:forward page="manage.jsp" />
<%
    }
    else {
        rs.close();
        DB.close(conn);
        DB.close(stmt);
%>
<jsp:forward page="login.jsp">
    <jsp:param name="warning" value="对不起,您的用户名或密码不正确" />
</jsp:forward>
</body>
<%
    }
%>
</html>

(8)delete.jsp

<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.Statement" %>
<%@ page import="java.sql.ResultSet" %><%--
  Created by IntelliJ IDEA.
  User: 12526
  Date: 2021/1/16
  Time: 19:38
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>删除投票项</title>
</head>
<body>
<jsp:useBean id="DB" class="com.gx.bean.conn" scope="session"/>
<%
    String delid = request.getParameter("delid");
    System.out.println(delid);
    Connection conn = DB.getConn();
    Statement stmt = DB.getStatement(conn);
    String sql = "delete from vote where id ="+delid;
    if(delid!=null){
        DB.excuteUpdate(stmt,sql);
    }
    DB.close(conn);
    DB.close(stmt);
%>
<jsp:forward page="manage.jsp" />
</body>
</html>

(9)checkadmin.jsp

<%--
  Created by IntelliJ IDEA.
  User: 12526
  Date: 2021/1/16
  Time: 20:10
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=utf-8" language="java"%>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%
    if(session.getAttribute("admin") == null){
        response.sendRedirect("login.jsp");
    }
%>
</body>
</html>

(10)info.jsp

<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.Statement" %>
<%@ page import="java.sql.ResultSet" %><%--
  Created by IntelliJ IDEA.
  User: 12526
  Date: 2021/1/15
  Time: 20:08
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>投票结果</title>
</head>
<body>
<jsp:useBean id="DB" class="com.gx.bean.conn" />
<center>
    <h2>投票结果</h2>
    <table border bordercolor="#0099ff">
        <tr>
            <th bgcolor="#CCFFFF">选项</th>
            <th bgcolor="#CCFFFF">得票数</th>
            <th bgcolor="#CCFFFF">比例</th>
        </tr>
        <%
            int totalNum =0;
            Connection conn = DB.getConn();
            Statement stmt = DB.getStatement(conn);
            ResultSet rs = DB.getResultSet(stmt,"select count from vote");
            while(rs.next()){
                totalNum += rs.getInt(1);
            }
            DB.close(rs);
            rs = DB.getResultSet(stmt,"select * from vote");
            while (rs.next()){
                out.println("<tr>");
                int num = rs.getInt("count");
                out.println("<td>"+rs.getString("item")+"</td>");
                out.println("<td>得:"+num+"票</td>");
                out.println("<td>得票率: ");
                out.println(num*100/totalNum+"%</td>");
            }
            DB.close(conn);
            DB.close(stmt);
            DB.close(rs);
        %>
    </table>
    <p><a href="index.jsp">返回投票页面</a></p>
</center>
</body>
</html>

(11)manage.jsp

<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.Statement" %>
<%@ page import="java.sql.ResultSet" %><%--
  Created by IntelliJ IDEA.
  User: 12526
  Date: 2021/1/16
  Time: 18:36
  To change this template use File | Settings | File Templates.
--%>
<%--<%@ include file="checkadmin.jsp"%>--%>
<%@ page contentType="text/html;charset=utf-8" language="java" %>
<%@ include file="checkadmin.jsp"%>
<html>
<head>
    <title>Title</title>
</head>
<body>
<jsp:useBean id="DB" class="com.gx.bean.conn" scope="session"/>
<center>
    <h2>系统维护</h2>
    <table border bordercolor="#0099FF">
        <tr>
            <th colspan="3" bgcolor="#CCFFFF">
                删除投票项
            </th>
            <%
                Connection conn = DB.getConn();
                Statement stmt = DB.getStatement(conn);
                ResultSet rs = DB.getResultSet(stmt,"select * from vote");
                int i = 1;
                while (rs.next()){
                    out.println("<tr>");
                    out.println("<td>"+i+"</td>");
                    out.println("<td>"+rs.getString("item")+"</td>");
                    out.println("<td align='center'><a href='delete.jsp?delid="+ rs.getString("id") +" '>删除</a></td>");
                    i++;
                }
                DB.close(rs);
                DB.close(conn);
                DB.close(stmt);
            %>
        </tr>
        <tr>
            <th colspan="3" bgcolor="#CCFFFF">
                添加投票项
            </th>
        </tr>
        <tr>
            <td colspan="3">
                <form action="add.jsp" method="post">
                    内容:
                    <input type="text" name="additem" size="30">
                    <input type="submit" value="提交">
                    <input type="reset" value="重置">
                </form>
            </td>
        </tr>
        <tr>
            <td colspan="3" align="center" bgcolor="#CCFFFF">
                <a href="index.jsp">返回投票页面</a>
            </td>
        </tr>
    </table>
</center>
</body>
</html>

项目结构目录
jsp实现一个简单的投票系统
数据库
jsp实现一个简单的投票系统
jsp实现一个简单的投票系统

上一篇:Java学习--JDBC


下一篇:1 JDBC是什么?