您好,登錄后才能下訂單哦!
這篇文章主要介紹“Java晚會抽獎系統如何實現”,在日常操作中,相信很多人在Java晚會抽獎系統如何實現問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Java晚會抽獎系統如何實現”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
環境配置:
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晚會抽獎系統如何實現”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。