javaweb项目案例投票系统
本博客只是记载自己学习javaweb历程
本项目用到了数据库的链接
不知道怎么链接数据库的可以参考一下
开发工具:myeclipse
数据库:mysql(Navicat for mySQL)
数据库代码与jsp写在同一页面了
先看一下最终效果
- jsp:2个jsp
- 一个是处理前台显示,一个是前台实现功能的业务处理逻辑
先看一下完整的项目目录
- index.jsp代码
<%@ page language="java" import="java.sql.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<h1>欢迎给老师投票</h1>
<table>
<tr>
<td>编号</td>
<td>姓名</td>
<td>得票数</td>
<td>投票</td>
</tr>
<%
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/teacherdb?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&useSSL=false&serverTimezone=UTC","root", "root");
Statement stat = conn.createStatement();
String sql =
"select teacherno,teachername,vote from t_vote";
ResultSet rs = stat.executeQuery(sql);
while(rs.next()){
String teacherno = rs.getString("teacherno");
String teachername = rs.getString("teachername");
int vote = rs.getInt("vote");
%>
<tr>
<td><%= teacherno %></td>
<td><%= teachername %></td>
<td><img alt="" src="img/bar.jpg" width="<%=vote %>"height="10"><%=vote %></td>
<td><a href="dealindex.jsp?teacherno=<%=teacherno%>">投票</a></td>
</tr>
<%
}
stat.close();
rs.close();
%>
</table>
</body>
</html>
- dealindex.jsp
<%@ page language="java" import="java.sql.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'dealindex.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
String teacherno = request.getParameter("teacherno");
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/teacherdb?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&useSSL=false&serverTimezone=UTC","root", "root");
String sql = "update t_vote set vote = vote + 1 where teacherno = ?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, teacherno);
ps.executeUpdate();
ps.close();
conn.close();
%>
<jsp:forward page="index.jsp"></jsp:forward>
</body>
</html>
bar.jpg图片自己用画图工具画一个就行了,大小没有限制。
有关软件下载,资源查找可以来看看小编的网站,谢谢
网址:www.miaozan.art