时间:2023-07-05 07:21:01 | 来源:网站运营
时间:2023-07-05 07:21:01 来源:网站运营
如何使用Java制作一个网页版的投票系统?:<% request.setCharacterEncoding("gb2312"); String account = request.getParameter("account"); String password = request.getParameter("password"); UserDao udao = new UserDao(); User user = udao.getUserByAccount(account); if(user == null||!user.getPassword().equals(password)){ %> <br><br><br><br><br><br> 登录失败,<a href="loginForm.jsp">返回登录页面</a> <% } else if(user.getAdmin()==true){ session.setAttribute("user", user); response.sendRedirect("admin.jsp"); } else{ session.setAttribute("user", user); response.sendRedirect("display.jsp"); } %>
投票模块代码<% String account=user.getAccount(); String[]teacherno =request.getParameterValues("teacherno"); VoteDao vdao = new VoteDao(); UserDao udao = new UserDao(); int ticket= udao.ticket(account); if(ticket>0){ if(teacherno!=null){ vdao.updateVotes(teacherno); vdao.updateTictet(account); } response.sendRedirect("display.jsp"); } else{ response.sendRedirect("noTicket.jsp"); } %>
新建用户模块<% request.setCharacterEncoding("gb2312"); String account = request.getParameter("account"); String password = request.getParameter("password"); String uname = request.getParameter("uname"); String admin =request.getParameter("admin"); UserDao udao = new UserDao(); User user2=udao.getUserByAccount(account); if(user2!=null){ %> <jsp:forward page="changeError2.jsp"></jsp:forward> <% } udao.adduser(account, password, uname, admin); %> <br><br><br><br><br><br> <p align="center">创建用户成功!<br> <a href="admin.jsp">返回</a></p>
删除用户模块<% request.setCharacterEncoding("gb2312"); String account = request.getParameter("account"); String acc = user.getAccount(); if(acc.equals(account)){ %> <jsp:forward page="changeError3.jsp"></jsp:forward> <% } UserDao udao = new UserDao(); User user2=udao.getUserByAccount(account); if(user2==null){ %><jsp:forward page="changeError.jsp"></jsp:forward> <% } udao.deluser(account); %> <br><br><br><br><br><br> <p align="center">删除用户成功!<br> <a href="admin.jsp">返回</a></p>
修改用户模块<% request.setCharacterEncoding("gb2312"); String account = request.getParameter("account"); String password = request.getParameter("password"); String uname = request.getParameter("uname"); String admin =request.getParameter("admin"); UserDao udao = new UserDao(); User user2=udao.getUserByAccount(account); if(user2==null){ %> <jsp:forward page="changeError.jsp"></jsp:forward> <% } udao.changeuser(account, password, uname, admin); %> <br><br><br><br><br><br> <p align="center">修改用户成功!<br> <a href="admin.jsp">返回管理页面</a></p>
初始化投票模块public void initVote() throws Exception{ initConnection(); String sql="UPDATE T_VOTE SET VOTE = 0"; PreparedStatement ps = conn.prepareStatement(sql); ps.executeUpdate(); closeConnection();} public void initTicket() throws Exception{ initConnection(); String sql="UPDATE T_USER SET TICKET = 1"; PreparedStatement ps = conn.prepareStatement(sql); ps.executeUpdate(); closeConnection();}
注销登录模块<% session.invalidate(); response.sendRedirect("loginForm.jsp"); %>
防刷票的实现public int ticket(String account) throws Exception{ initConnection(); String sql ="SELECT TICKET FROM T_USER WHERE ACCOUNT= ? "; PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1, account); ResultSet rs = ps.executeQuery(); rs.next(); int ticket =rs.getInt("TICKET"); if(ticket==0) { closeConnection(); return 0; } else { closeConnection(); return 1; } }
public void initConnection() throws Exception { Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); conn=DriverManager.getConnection("jdbc:sqlserver://localhost: 1433;DatabaseName=Vote;user=root;password=123456789");
public User getUserByAccount(String account) throws Exception{ User use = null; initConnection(); String sql="SELECT ACCOUNT,PASSWORD,UNAME,ADMIN FROM T_USER WHERE ACCOUNT = ?"; PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1, account); ResultSet rs =ps.executeQuery(); if(rs.next()) { use = new User(); use.setAccount(rs.getString("ACCOUNT")); use.setPassword(rs.getString("PASSWORD")); use.setUname(rs.getString("UNAME")); use.setAdmin(rs.getBoolean("ADMIN")); } closeConnection(); return use;
关键词:系统,投票,使用