本文实例为大家分享了javaweb实现投票系统的具体代码,供大家参考,具体内容如下
这次给大家带来一个课堂基础作业,简单的javaweb投票系统。
当然也设计了添加投票人的接口,只是list页面没有直接导航过去。
如页面显示,可以增加和减少相应的票数。同时id是利用数据库该字段作为主键自增的。 name 和头像都是上传的,由mysql数据库存储照片名然后再自动寻找到。票数是直接由user_votes存储使用。
接下来带来关键代码。
//这是类User_list package com.example.user_votes; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; import java.sql.Driver; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; import java.util.ArrayList; import java.util.List; @WebServlet("/list") public class User_list extends HelloServlet{ @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //连接数据库,获取所有的用户信息 //创建一个用户对象 Listuserlist = new ArrayList (); try { // 1.加载驱动程序 // DriverManager.registerDriver(new com.mysql.cj.jdbc.Driver()); Class.forName("com.mysql.cj.jdbc.Driver"); // 2.创建数据库连接 String url = "jdbc:mysql://localhost:3306/java_test?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC"; String jdbcuser = "root"; String jdbcpwd = "500400"; Connection conn = DriverManager.getConnection(url, jdbcuser, jdbcpwd); // 3.创建语句对象 Statement stmt = conn.createStatement(); // 4.执行sql // 被注释的代码是插入数据 String sql ="SELECT * from user_votes"; //动态操作 ResultSet rows = stmt.executeQuery(sql); // 5.处理结果 rows.next() 用于遍历rows对象集 while(rows.next()){ User user = new User(); user.setId(rows.getInt("id")); user.setName(rows.getString("user_name")); user.setUrl(rows.getString("url")); user.setVotes(Integer.parseInt(rows.getString("votes"))); userlist.add(user); } // 6.关闭语句对象,数据库连接rows.close()关闭row对象集 rows.close(); stmt.clearBatch(); conn.close(); } catch (Exception e) { System.out.println("获取用户列表失败" + e.getMessage()); e.printStackTrace(); } //返回响应,以表格的方式显示所有用户 resp.setContentType("text/html"); resp.setCharacterEncoding("utf-8"); PrintWriter out =resp.getWriter(); out.println(" 投票系统 "); if (userlist.size()>0){ out.println(""); out.println("
"); out.println(""); }else{ out.println("暂无用户"); } out.println(); out.println(" } }"); out.println(" "); out.println(" "); for (User user:userlist){ out.println("id "); out.println("Name "); out.println("照片 "); out.println("votes "); out.println("操作 "); out.println(""); out.println(" "); } out.println(""+user.getId()+" "); out.println(""+"   "+user.getName()+" "); out.println(""); out.println(" "+"   "+user.getVotes()+" "); out.println("add 删除 "); out.println("
这一段是用于list页面的展示代码。
同时以下是增减票数的代码。
//这个是增加票数add package com.example.user_votes; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; import java.sql.*; @WebServlet("/add") public class AddUserVotes extends HttpServlet { @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //解析表单数据,获取要添加的用户信 // 解析请求 req.setCharacterEncoding("utf-8"); PrintWriter out = resp.getWriter(); //使用自己写的User类 User user = new User(); user.setId(Integer.parseInt(req.getParameter("id"))); user.setVotes(Integer.parseInt(req.getParameter("votes"))); // 连接数据库的操作 try { // 1.加载驱动程序 String driver = "com.mysql.cj.jdbc.Driver"; Class.forName(driver).newInstance(); // 2.创建数据库连接 String url = "jdbc:mysql://localhost:3306/java_test?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC"; String jdbcuser = "root"; String jdbcpwd = "500400"; Connection conn = DriverManager.getConnection(url,jdbcuser,jdbcpwd); // 3.创建语句对象 //静态的mysql操作语句 // Statement stmt = conn.createStatement(); // 4.执行sql String sql = "UPDATE user_votes SET votes = ? WHERE id = ?"; PreparedStatement pstmt = conn.prepareStatement(sql); //动态操作 int uservotes = user.getVotes()+1; pstmt.setInt(1,uservotes); pstmt.setInt(2,user.getId()); int rows = pstmt.executeUpdate(); // 5.处理结果 if(rows == 1){ System.out.println("add成功为:"+uservotes); } else { System.out.println("add失败!"); } // 6.关闭语句对象,数据库连接 pstmt.clearBatch(); conn.close(); } catch (Exception e) { System.out.println("添加用户失败"+e.getMessage()); e.printStackTrace(); }; resp.setContentType("text/html;charset=UTF-8"); resp.getWriter().println(""); //跳转到用户列表界面 resp.sendRedirect("list"); } }
减少票数
package com.example.user_votes; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; @WebServlet("/delete") public class DeleteUserVotes extends HelloServlet{ @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //解析表单数据,获取要添加的用户信 // 解析请求 req.setCharacterEncoding("utf-8"); PrintWriter out = resp.getWriter(); //使用自己写的User类 User user = new User(); user.setId(Integer.parseInt(req.getParameter("id"))); user.setVotes(Integer.parseInt(req.getParameter("votes"))); // 连接数据库的操作 try { // 1.加载驱动程序 String driver = "com.mysql.cj.jdbc.Driver"; Class.forName(driver).newInstance(); // 2.创建数据库连接 String url = "jdbc:mysql://localhost:3306/java_test?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC"; String jdbcuser = "root"; String jdbcpwd = "500400"; Connection conn = DriverManager.getConnection(url,jdbcuser,jdbcpwd); // 3.创建语句对象 //静态的mysql操作语句 // Statement stmt = conn.createStatement(); // 4.执行sql String sql = "DELETE FROM user_votes WHERE id = ?"; PreparedStatement pstmt = conn.prepareStatement(sql); //动态操作 pstmt.setInt(1,user.getId()); int rows = pstmt.executeUpdate(); // 5.处理结果 if(rows == 1){ System.out.println("delete成功为:"+user.getId()); } else { System.out.println("delete失败!"); } // 6.关闭语句对象,数据库连接 pstmt.clearBatch(); conn.close(); } catch (Exception e) { System.out.println("添加用户失败"+e.getMessage()); e.printStackTrace(); }; //跳转到用户列表界面 resp.sendRedirect("list"); } }
添加参与投票的
package com.example.user_votes; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; public class AddUser extends HelloServlet{ @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //解析表单数据,获取要添加的用户信 // 解析请求 req.setCharacterEncoding("utf-8"); PrintWriter out = resp.getWriter(); //使用自己写的User类 User user = new User(); user.setName(req.getParameter("name")); user.setUrl(req.getParameter("url")); user.setVotes(0); // 连接数据库的操作 try { // 1.加载驱动程序 String driver = "com.mysql.cj.jdbc.Driver"; Class.forName(driver).newInstance(); // 2.创建数据库连接 String url = "jdbc:mysql://localhost:3306/java_test?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC"; String jdbcuser = "root"; String jdbcpwd = "500400"; Connection conn = DriverManager.getConnection(url,jdbcuser,jdbcpwd); // 3.创建语句对象 //静态的mysql操作语句 // Statement stmt = conn.createStatement(); // 4.执行sql String sql = "INSERT INTO user_votes(user_name,url,votes) VALUES(?,?,?)\n"; PreparedStatement pstmt = conn.prepareStatement(sql); //动态操作 pstmt.setString(1,user.getName()); pstmt.setString(2,user.getUrl()); pstmt.setInt(3,user.getVotes()); int rows = pstmt.executeUpdate(); // 5.处理结果 if(rows == 1){ System.out.println("add成功为:"+user.getName()); } else { System.out.println("add失败!"); } // 6.关闭语句对象,数据库连接 pstmt.clearBatch(); conn.close(); } catch (Exception e) { System.out.println("添加用户失败"+e.getMessage()); e.printStackTrace(); }; //跳转到用户列表界面 resp.sendRedirect("list"); } }
该项目没有使用mvc项目结构,就是因为比较简单,因此如果有刚刚学习到这里的可以来看看,参考学习
附上项目目录图,其他的就没有过多的去介绍了,毕竟比较简单。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。