1。这里会连接数据库--JDBC的学习实例

一共有3个页面。

2.第一个页面是一个form表单,第二个页面是处理数据,第三个页面是显示页面

vote.jsp

  1. <body bgcolor="green">
  2.  
  3. 选择你要投票的人:
  4. <form action="vote_end.jsp">
  5. <input type="radio" name="pp" value="a"/>周杰伦<img src="img/a.jpg"/>
  6. <br><input type="radio" name="pp" value="b"/>张 杰<img src="img/b.jpg"/>
  7. <br><input type="radio" name="pp" value="c"/>范冰冰<img src="img/c.jpg"/>
  8. <br><input type="radio" name="pp" value="d"/>赵 薇<img src="img/d.jpg"/>
  9. <br><input type="radio" name="pp" value="e"/>黄晓明<img src="img/e.jpg"/>
  10. <br><br><input type="submit" value="提交"/>
  11. </form>
  12. </body>

  vote_end2.jsp

  1. <body bgcolor="red">
  2. <center>
  3. <%
  4. String sess = request.getSession().getId();
  5. String sess2 = null;
  6.  
  7. out.print("恭喜你,投票成功。<br>");
  8.  
  9. String pp = request.getParameter("pp");
  10. String people = null;
  11. if(pp.equals("a")){
  12. people="'周杰伦'";
  13. }else if(pp.equals("b")){
  14. people="'张杰'";
  15. }else if(pp.equals("c")){
  16. people="'范冰冰'";
  17. }else if(pp.equals("d")){
  18. people="'赵薇'";
  19. }else if(pp.equals("e")){
  20. people="'黄晓明'";
  21. }
  22.  
  23. Class.forName("com.mysql.jdbc.Driver");
  24. Connection connection=DriverManager.getConnection("jdbc:mysql://localhost/test?user=root&password=123&useUnicode=true&characterEncoding=gbk");
  25. Statement statement = connection.createStatement();
  26.  
  27. //查看是否投过票
  28. ResultSet rs2 = statement.executeQuery("SELECT * FROM sess");
  29. while(rs2.next()){
  30. sess2 = rs2.getString("id");
  31. if(sess2.equals(sess)){
  32. %>
  33. <jsp:forward page="vote_no.jsp"/>
  34. <%
  35. }
  36. }
  37.  
  38. //查找数据库
  39. ResultSet rs = statement.executeQuery("SELECT * FROM people where name="+people);
  40. rs.next();
  41. int count = rs.getInt("count");
  42. count = count+1;
  43. //更新数据库
  44. statement.executeUpdate("UPDATE people SET count="+count+" where name="+people);
  45.  
  46. //投票session号保存到数据库
  47.  
  48. statement.executeUpdate("insert into sess values('"+sess+"')");
  49.  
  50. //显示数据库
  51. ResultSet rss = statement.executeQuery("SELECT * FROM people");
  52. out.print("<table border=1>");
  53. out.print("<tr>");
  54. out.print("<th>姓名</th>");
  55. out.print("<th>票数</th>");
  56. out.print("</tr>");
  57. while(rss.next()) {
  58. out.print("<tr>");
  59. out.print("<td>"+rss.getString(1)+"</td>");
  60. out.print("<td>"+rss.getString(2)+"</td>");
  61. out.print("</tr>");
  62. }
  63. out.print("</table>");
  64. rs.close();
  65. statement.close();
  66. connection.close();
  67.  
  68. %>
  69.  
  70. </center>
  71. </body>

  vote_end.jsp

  1. <body bgcolor="red">
  2. <center>
  3. <%
  4.  
  5. out.print("恭喜你,投票成功。<br>");
  6.  
  7. String pp = request.getParameter("pp");
  8. String people = null;
  9. if(pp.equals("a")){
  10. people="'周杰伦'";
  11. }else if(pp.equals("b")){
  12. people="'张杰'";
  13. }else if(pp.equals("c")){
  14. people="'范冰冰'";
  15. }else if(pp.equals("d")){
  16. people="'赵薇'";
  17. }else if(pp.equals("e")){
  18. people="'黄晓明'";
  19. }
  20.  
  21. Class.forName("com.mysql.jdbc.Driver");
  22. Connection connection=DriverManager.getConnection("jdbc:mysql://localhost/test?user=root&password=123&useUnicode=true&characterEncoding=gbk");
  23. Statement statement = connection.createStatement();
  24. //查找数据库
  25. ResultSet rs = statement.executeQuery("SELECT * FROM people where name="+people);
  26. rs.next();
  27. int count = rs.getInt("count");
  28. count = count+1;
  29. //更新数据库
  30. statement.executeUpdate("UPDATE people SET count="+count+" where name="+people);
  31.  
  32. //显示数据库
  33. ResultSet rss = statement.executeQuery("SELECT * FROM people");
  34. out.print("<table border=1>");
  35. out.print("<tr>");
  36. out.print("<th>姓名</th>");
  37. out.print("<th>票数</th>");
  38. out.print("</tr>");
  39. while(rss.next()) {
  40. out.print("<tr>");
  41. out.print("<td>"+rss.getString(1)+"</td>");
  42. out.print("<td>"+rss.getString(2)+"</td>");
  43. out.print("</tr>");
  44. }
  45. out.print("</table>");
  46. rs.close();
  47. statement.close();
  48. connection.close();
  49.  
  50. %>
  51. </center>
  52. </body>

  

java web 程序---投票系统的更多相关文章

  1. Java Web程序工作原理

    Web开发的最重要的基本功能是HTTP:Java Web开发的最重要的基本功是Servlet Specification.HTTP和Servlet Specitication对于Web Server和 ...

  2. 在Java Web程序中使用监听器可以通过以下两种方法

    之前学习了很多涉及servlet的内容,本小结我们说一下监听器,说起监听器,编过桌面程序和手机App的都不陌生,常见的套路都是拖一个控件,然后给它绑定一个监听器,即可以对该对象的事件进行监听以便发生响 ...

  3. 在Java Web程序中使用Hibernate

    在Java Web程序中使用Hibernate与普通Java程序一样.本文中将使用Servlet和JSP结合Hibernate实现数据库表的增删改查操作. Web程序中,hibernate.cfg.x ...

  4. java Web程序使用wro4j合并、压缩js、css等静态资源

    在Web项目中,js.css合并压缩,不仅有利于减少Http请求数量.减少宽带资源占用,还能有效的管理各种js.css的引入,使整个项目更加有序.而对于访问用户来说,其更大的好处是增加了页面的打开速度 ...

  5. @Java web程序员,在保留现场,服务不重启的情况下,执行我们的调试代码(JSP 方式)

    一.前言 类加载器实战系列的第六篇(悄悄跟你说,这篇比较水),前面5篇在这里: 实战分析Tomcat的类加载器结构(使用Eclipse MAT验证) 还是Tomcat,关于类加载器的趣味实验 了不得, ...

  6. linux tomcat部署含有matlab画图打包的java web程序

    首先说下问题:matlab可以把相关算法代码打包成jar文件共java调用,本例使用的jar文件的作用是画图并保存,然后部署在linux的tomcat中进行发布.这里出现了一个问题,具体如下:linu ...

  7. Spring MVC + Spring + Mybitis开发Java Web程序基础

    Spring MVC + Spring + Mybitis是除了SSH外的另外一种常见的web框架组合. Java web开发和普通的Java应用程序开发是不太一样的,下面是一个Java web开发在 ...

  8. 新浪云部署java web程序 注意事项

    在新浪云新手指南里有部署java的示例,但是对一个新手来说难免会有一些地方犯错,折腾了好长时间才把自己的java web部署到了新浪云.这里主要写一些我遇到的问题与第一次使用新浪云的朋友分享一下. 首 ...

  9. 使用spring框架创建最简单的java web程序(IDEA商业版)

    项目目录如下(IDEA社区版好像无法识别webapp目录?原因见https://www.cnblogs.com/bityinjd/p/9284378.html): 工具:  IDEA 1.首先使用ma ...

随机推荐

  1. Allow Zero Length 允许空字符串 ACCESS

    http://www.360doc.com/content/11/0118/20/991597_87447868.shtml https://microsoft.public.data.ado.nar ...

  2. python关于时间的计算,time模块

    import time, datetime # time.time 返回当前时间的时间戳(1970纪元后经过的浮点秒数)(格林尼治时间). # 1分钟60秒,1小时3600秒,1天86400秒. pr ...

  3. python笔记01:基础知识

    1.4 数字和表达式 # -*- coding:utf-8 -*- #1.4 #除法 print 1 / 2 print 1.0 / 2 print 10 / 3 print 10.0 / 3.0 p ...

  4. 静态函数和全局函数都没有this指针

    静态函数和全局函数都没有this指针

  5. sqljdbc 无法连接到主机

    今天发现sqljdbc 无法连接到SQL Server 主机.原因是SQL Server Browser 服务没有打开

  6. HDTunePro汉化版温度显示不正常后需要更改

    查找:2564B046000000002564B043替换:2564A848000000002564A1E6 查找:B0432F2564B046替换:A1E62F2564A848

  7. ACCESS表的视图

    ACCESS表的视图 点击:   发布日期:2007-8-31 6:37:00   进入论坛     表是关系型数据库的基本结构.在Access中,表是一种关系特定主题的数据集合,如产品.供应商等.为 ...

  8. memmove 对同一个指针不操作

    memmove 对同一个指针不操作,所以调用memmove之前不用比较两个指针是否相同 void CTestDLLDlg::OnBnClickedButton6() { ; char* data = ...

  9. Spring学习(二)--IOC

    一.什么是IOC? 孤傲苍狼总结的理解: https://www.cnblogs.com/xdp-gacl/p/4249939.html 我的理解(不知道对不对哈,不对的话请各位大神指出): IOC往 ...

  10. ubuntu vsftpd

    With a bit of playing around I've managed to come up with a semi solution (not perfect but good enou ...