Java抽奖系统,java抽奖程序
目录
一、项目运行二、效果图三、核心代码管理员登陆控制层抽奖控制层一二三等奖设置抢答控制层
一、项目运行
环境配置:
JDK 1.8 Tomcat 8.5 MySQL Eclispe(IntelliJ IDEA,Eclispe,MyEclispe,Sts都支持)
项目技术:
JDBC Servlert html CSS JavaScript JQuery Ajax等等
二、效果图
三、核心代码
管理员登陆控制层
公共类管理操作实现模型驱动的{ admin admin=null public String log in(){ db manager db manager=new db manager();字符串SQL= select * from admin,其中username= admin。get username() 和password= admin。获取密码()“”;结果集结果集=数据库管理器。执行查询(SQL);请尝试{ if(resultset。next()){ system。出去。println(登陆成功);ActionContext.getContext().getSession().put(admin ,admin);返回"成功";}else {ActionContext.getContext().getSession().放(消息,登录失败);返回"失败";}}捕捉(异常e){ e . printstacktrace();ActionContext.getContext().getSession().放(消息,登录失败);//todo :处理异常}最后{ db manager。关闭连接();}返回"失败";} @ override公共对象get model(){//TODO自动生成的方法stub if(admin==null){ admin=new admin();}返回管理员}}
抽奖控制层一二三等奖设置
公共类稠江行动实现模型驱动{ private luck _ dog luck _ dog=null@ override public Object get model(){//TODO自动生成方法stub if(luck _ dog==null){ luck _ dog=new luck _ dog();}还运气_狗;} public void Chou Jiang(){ system .出去。println(幸运_狗。get luck _ num());db manager db manager=new db manager();string SQL= select * from lucky _ dog where luck _ num= luck _ dog。get luck _ num();结果集结果集=数据库管理器。执行查询(SQL);请尝试{ if(resultset。next()){ system。出去。println(这个号码已经中过奖!);字符串s={name: 提示 , age : alreadyexisterror } ;servletactioncontext。getresponse().setCharacterEncoding(“UTF-8”);servletactioncontext。getresponse().getWriter().印刷品;返回;} else { int total=0;int luck_gr
oup = 0;sql = "select count(*)as totalCount from lucky_dog";resultSet = dbManager.executeQuery(sql);if (resultSet.next()) {total = resultSet.getInt("totalCount");}// 得到抽奖表中的记录数if (total < 8) {luck_group = 3;// 三等奖8个} else if (total < 13) {luck_group = 2;// 二等奖5个} else if (total < 16) {luck_group = 1;// 一等奖3个} else {System.out.println("抽奖总数已满!!!");String s = "{"name":"提示","age":"totaloverflowerror"}";ServletActionContext.getResponse().setCharacterEncoding("UTF-8");ServletActionContext.getResponse().getWriter().print(s);return;}sql = "insert into lucky_dog (luck_num,luck_group)values("+ luck_dog.getLuck_num() + "," + luck_group + ")";boolean result = dbManager.insertSQL(sql);if (result) {String s = "{"name":"提示","age":"success"}";ServletActionContext.getResponse().setCharacterEncoding("UTF-8");ServletActionContext.getResponse().getWriter().print(s);System.out.println("抽奖数据已经写入数据库");} else {System.out.println("抽奖数据写入失败");}}} catch (Exception e) {// TODO: handle exception} finally {dbManager.closeConnection(); } } public void view_result() {System.out.println("view_resultview_resultview_result");DBManager dbManager = new DBManager();String sql = "select * from lucky_dog";luck_dog luck_dog = null;ResultSet resultSet = dbManager.executeQuery(sql);List<luck_dog> luck_dogList = new ArrayList<luck_dog>();try {while (resultSet.next()) {luck_dog = new luck_dog();luck_dog.setId(resultSet.getInt("id"));luck_dog.setLuck_group(resultSet.getInt("luck_group"));luck_dog.setLuck_num(resultSet.getInt("luck_num"));luck_dogList.add(luck_dog);}JSONArray jsonArray = JSONArray.fromObject(luck_dogList);System.out.println(jsonArray);// String s = "{"name":"提示","age":"已提交抢答申请,请静候抢答结果!!!"}";ServletActionContext.getResponse().setCharacterEncoding("UTF-8");ServletActionContext.getResponse().getWriter().print(jsonArray);} catch (Exception e) {// TODO: handle exceptione.printStackTrace();} finally {dbManager.closeConnection(); } } }
抢答控制层
public class choujiangAction implements ModelDriven {private luck_dog luck_dog = null; @Overridepublic Object getModel() {// TODO Auto-generated method stubif (luck_dog == null) {luck_dog = new luck_dog();}return luck_dog;} public void choujiang() {System.out.println(luck_dog.getLuck_num());DBManager dbManager = new DBManager();String sql = "select * from lucky_dog where luck_num = "+ luck_dog.getLuck_num();ResultSet resultSet = dbManager.executeQuery(sql);try {if (resultSet.next()) {System.out.println("这个号码已经中过奖!!!");String s = "{"name":"提示","age":"alreadyexisterror"}";ServletActionContext.getResponse().setCharacterEncoding("UTF-8");ServletActionContext.getResponse().getWriter().print(s);return;} else {int total = 0;int luck_group = 0;sql = "select count(*)as totalCount from lucky_dog";resultSet = dbManager.executeQuery(sql);if (resultSet.next()) {total = resultSet.getInt("totalCount");}// 得到抽奖表中的记录数if (total < 8) {luck_group = 3;// 三等奖8个} else if (total < 13) {luck_group = 2;// 二等奖5个} else if (total < 16) {luck_group = 1;// 一等奖3个} else {System.out.println("抽奖总数已满!!!");String s = "{"name":"提示","age":"totaloverflowerror"}";ServletActionContext.getResponse().setCharacterEncoding("UTF-8");ServletActionContext.getResponse().getWriter().print(s);return;}sql = "insert into lucky_dog (luck_num,luck_group)values("+ luck_dog.getLuck_num() + "," + luck_group + ")";boolean result = dbManager.insertSQL(sql);if (result) {String s = "{"name":"提示","age":"success"}";ServletActionContext.getResponse().setCharacterEncoding("UTF-8");ServletActionContext.getResponse().getWriter().print(s);System.out.println("抽奖数据已经写入数据库");} else {System.out.println("抽奖数据写入失败");}}} catch (Exception e) {// TODO: handle exception} finally {dbManager.closeConnection(); } } public void view_result() {System.out.println("view_resultview_resultview_result");DBManager dbManager = new DBManager();String sql = "select * from lucky_dog";luck_dog luck_dog = null;ResultSet resultSet = dbManager.executeQuery(sql);List<luck_dog> luck_dogList = new ArrayList<luck_dog>();try {while (resultSet.next()) {luck_dog = new luck_dog();luck_dog.setId(resultSet.getInt("id"));luck_dog.setLuck_group(resultSet.getInt("luck_group"));luck_dog.setLuck_num(resultSet.getInt("luck_num"));luck_dogList.add(luck_dog);}JSONArray jsonArray = JSONArray.fromObject(luck_dogList);System.out.println(jsonArray);// String s = "{"name":"提示","age":"已提交抢答申请,请静候抢答结果!!!"}";ServletActionContext.getResponse().setCharacterEncoding("UTF-8");ServletActionContext.getResponse().getWriter().print(jsonArray);} catch (Exception e) {// TODO: handle exceptione.printStackTrace();} finally {dbManager.closeConnection(); } } }
到此这篇关于Java实战之晚会抽奖系统的实现的文章就介绍到这了,更多相关Java抽奖系统内容请搜索盛行IT以前的文章或继续浏览下面的相关文章希望大家以后多多支持盛行IT!
郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。