文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

Java实战之晚会抽奖系统的实现

2024-04-02 19:55

关注

一、项目运行

环境配置:

Jdk1.8 + Tomcat8.5 + mysql + Eclispe(IntelliJ IDEA,Eclispe,MyEclispe,Sts都支持)

项目技术:

Jdbc+ Servlert + html+ css + JavaScript + JQuery + Ajax +等等

二、效果图

三、核心代码

管理员登陆控制层

public class adminAction implements ModelDriven{
	admin admin = null;
	public String login() {
		DBManager dbManager = new DBManager();
		String sql= "select * from admin where username = '"+admin.getUsername()+"' and password = '"+admin.getPassword()+"'";
		ResultSet resultSet = dbManager.executeQuery(sql);
		try {
			if (resultSet.next()) {
			System.out.println("登陆成功");
			ActionContext.getContext().getSession().put("admin", admin);
			return "success";
		}else {
			ActionContext.getContext().getSession().put("message", "登录失败");
			return "failed";
		}
		} catch (Exception e) {
			e.printStackTrace();
			ActionContext.getContext().getSession().put("message", "登录失败");
			// TODO: handle exception
		}finally{
			dbManager.closeConnection();
		}
		
		return "failed";
	}
 
	@Override
	public Object getModel() {
		// TODO Auto-generated method stub
		if (admin == null) {
			admin = new admin();
		}
		return admin;
	}
}

抽奖控制层一二三等奖设置

public class choujiangAction implements ModelDriven {
	private luck_dog luck_dog = null;
 
	@Override
	public Object getModel() {
		// TODO Auto-generated method stub
		if (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 exception
			e.printStackTrace();
		} finally {
			dbManager.closeConnection();
 
		}
 
	}
 
}

抢答控制层

public class choujiangAction implements ModelDriven {
	private luck_dog luck_dog = null;
 
	@Override
	public Object getModel() {
		// TODO Auto-generated method stub
		if (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 exception
			e.printStackTrace();
		} finally {
			dbManager.closeConnection();
 
		}
 
	}
 
}

到此这篇关于Java实战之晚会抽奖系统的实现的文章就介绍到这了,更多相关Java抽奖系统内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     807人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     351人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     314人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     433人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     221人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-后端开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯