0


Java项目:晚会抽奖系统(java+Jdbc+Servlet+Ajax+mysql)

源码获取:博客首页 "资源" 里下载!

一、项目运行 环境配置:

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 qiangdaAction {

    public void start_qinagda() {
        DBManager dbManager = new DBManager();
        // 设置之前所有的抢答结束
        String sql = "update qiangda_group set is_end = 1 where 1=1";
        System.out.println(dbManager.executeUpdate(sql));
        sql = "insert into qiangda_group (create_time,started,is_end)values('"
                + DateUtil.GetTime() + "',1,0)";
        //
        System.out.println(sql);
        dbManager.insertSQL(sql);
        // System.out.println(dbManager.insertSQL(sql));
        // long a = Calendar.getInstance().getTimeInMillis();
        // System.err.println(a);

        try {
            String s = "{\"name\":\"提示\",\"age\":\"新一轮抢答开始!!!\"}";

            ServletActionContext.getResponse().setCharacterEncoding("UTF-8");
            ServletActionContext.getResponse().getWriter().print(s);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            dbManager.closeConnection();

        }

    }

    public void qiangda() {
        DBManager dbManager = new DBManager();
        String sqlString = "select * from qiangda_group where is_end =0";
        System.err.println(sqlString);
        String ip = ServletActionContext.getRequest().getParameter("ip");
        System.out.println(ip);
        ResultSet resultSet = dbManager.executeQuery(sqlString);
        qiangda_group qiangda_group = null;
        try {
            if (resultSet.next()) {
                qiangda_group = new qiangda_group();
                qiangda_group.setCreate_time(resultSet.getDate("create_time"));
                qiangda_group.setGroup_id(resultSet.getInt("group_id"));
                qiangda_group.setIs_end(resultSet.getInt("is_end"));
                qiangda_group.setStarted(resultSet.getInt("started"));
                System.out.println(qiangda_group);
                // 插入新纪录之前要查询是否已经抢答过了
                String sql = "select * from qiangda where ip = '" + ip
                        + "' and group_id =" + qiangda_group.getGroup_id();
                resultSet = dbManager.executeQuery(sql);
                if (resultSet.next()) {
                    System.out.println("本轮已经抢答过了!!!");
                    String s = "{\"name\":\"提示\",\"age\":\"本轮已经抢答过了!!!\"}";
                    ServletActionContext.getResponse().setCharacterEncoding(
                            "UTF-8");
                    ServletActionContext.getResponse().getWriter().print(s);
                } else {
                    sql = "insert into qiangda (group_id,ip,qiangda_time)values ("
                            + qiangda_group.getGroup_id()
                            + ",'"
                            + ip
                            + "','"
                            + String.valueOf(Calendar.getInstance()
                                    .getTimeInMillis()) + "')";
                    System.err.println(sql);
                    dbManager.insertSQL(sql);
                    System.out.println("抢答成功!!!");
                    String s = "{\"name\":\"提示\",\"age\":\"已提交抢答申请,请静候抢答结果!!!\"}";
                    ServletActionContext.getResponse().setCharacterEncoding("UTF-8");
                    ServletActionContext.getResponse().getWriter().print(s);
                }
            }
        } catch (Exception e) {
            // TODO: handle exception
        } finally {
            dbManager.closeConnection();

        }

    }

    public void view_latest_result() {
        DBManager dbManager = new DBManager();
        String sqlString = "select max(group_id) as max_id from qiangda_group ";
        try {
            System.out.println(sqlString);
            ResultSet resultSet = dbManager.executeQuery(sqlString);
            if (resultSet.next()) {
                int max_id = resultSet.getInt("max_id");
                sqlString = "select * from qiangda where group_id = "+max_id+" order by qiangda_time";
                System.out.println(sqlString);
                resultSet = dbManager.executeQuery(sqlString);
                List<qiangda> qiangdaList = new ArrayList<qiangda>();
                qiangda qiangda = null;
                while (resultSet.next()) {
                    qiangda = new qiangda();
                    qiangda.setGroup_id(resultSet.getInt("group_id"));
                    qiangda.setIp(resultSet.getString("ip"));
                    qiangda.setQiangda_id(resultSet.getInt("qiangda_id"));
                    qiangda.setQiangda_time( Long.parseLong(resultSet.getString("qiangda_time")));
                    qiangdaList.add(qiangda);
                    
                }
                JSONArray jsonArray = JSONArray.fromObject(qiangdaList);  
                
                ActionContext.getContext().getSession().put("qiangdaList", qiangdaList);
                System.out.println(jsonArray);
                //String s = "{\"name\":\"提示\",\"age\":\"已提交抢答申请,请静候抢答结果!!!\"}";
                ServletActionContext.getResponse().setCharacterEncoding("UTF-8");
                ServletActionContext.getResponse().getWriter().print(jsonArray);
                /*
                 [
                 {"qiangda_id":15,"group_id":56,"qiangda_time":1386253774144,"ip":"0:0:0:0:0:0:0:1"}
                 ,
                 {"qiangda_id":16,"group_id":56,"qiangda_time":1386253977924,"ip":"192.168.43.176"}
                 ,
                 {"qiangda_id":17,"group_id":56,"qiangda_time":1386253988012,"ip":"192.168.24.70"}
                 ]
                 * 
                 * {\"name\":\"提示\",\"age\":\"已提交抢答申请,请静候抢答结果!!!\"}
                 * */
            }
            
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        } finally {
            dbManager.closeConnection();

        }

    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        qiangda qiangda = new qiangda ();
        qiangda.setGroup_id(1);
        qiangda.setIp("127.0.0.1");
        qiangda.setQiangda_id(12);
        qiangda.setQiangda_time((Long.getLong("1245792323546")));
        JSONArray jsonArray = JSONArray.fromObject(qiangda); 
        System.out.println(jsonArray);
    }

}

源码获取:博客首页 "资源" 里下载!

标签: java mysql ajax

本文转载自: https://blog.csdn.net/pastclouds/article/details/123478431
版权归原作者 OldWinePot 所有, 如有侵权,请联系我们删除。

“Java项目:晚会抽奖系统(java+Jdbc+Servlet+Ajax+mysql)”的评论:

还没有评论