JSP MVC模式案例之投票系统
【摘要】
create table voteitems( id number(8) primary key, item varchar2(50) not null, times number(8) default 0); create sequence seq_voteitems; insert into voteitems(id,item) v...
-
create table voteitems(
-
id number(8) primary key,
-
item varchar2(50) not null,
-
times number(8) default 0
-
);
-
-
create sequence seq_voteitems;
-
-
insert into voteitems(id,item) values (seq_voteitems.nextval,'jsp');
-
insert into voteitems(id,item) values (seq_voteitems.nextval,'asp');
-
insert into voteitems(id,item) values (seq_voteitems.nextval,'asp.net');
-
insert into voteitems(id,item) values (seq_voteitems.nextval,'php');
-
-
-
commit;
-
package com.javaweb.dao.util;
-
-
import java.sql.Connection;
-
import java.sql.DriverManager;
-
import java.sql.ResultSet;
-
import java.sql.SQLException;
-
import java.sql.Statement;
-
-
public class BaseDao {
-
private static String url = "jdbc:oracle:thin:@localhost:1521:orcl";
-
private static String driver = "oracle.jdbc.driver.OracleDriver";
-
private static String user="scott";
-
private static String pwd = "tiger";
-
-
public static Connection getConnection()
-
throws ClassNotFoundException, SQLException{
-
Class.forName(driver);
-
Connection con = DriverManager.getConnection(url,user,pwd);
-
return con;
-
}
-
-
public static void closeAll(ResultSet rs,Statement stmt,Connection con)
-
throws SQLException{
-
if(rs!=null)
-
rs.close();
-
if(stmt!=null)
-
stmt.close();
-
if(con!=null)
-
con.close();
-
}
-
}
-
package com.javaweb.dao;
-
-
import java.util.List;
-
-
import com.javaweb.entity.VoteItems;
-
-
public interface VoteItemsDao {
-
public List<VoteItems> findAll();
-
public boolean update(int id);
-
}
-
package com.javaweb.dao.impl;
-
-
import java.sql.Connection;
-
import java.sql.PreparedStatement;
-
import java.sql.ResultSet;
-
import java.sql.SQLException;
-
import java.util.ArrayList;
-
import java.util.List;
-
-
import com.javaweb.dao.VoteItemsDao;
-
import com.javaweb.dao.util.BaseDao;
-
import com.javaweb.entity.VoteItems;
-
-
public class VoteItemsDaoImpl extends BaseDao implements VoteItemsDao {
-
-
-
public static void main(String[] args) {
-
System.out.println(new VoteItemsDaoImpl().findAll().size());
-
}
-
-
-
public List<VoteItems> findAll() {
-
Connection con = null;
-
PreparedStatement pstmt = null;
-
ResultSet rs=null;
-
List<VoteItems> items=null;
-
String sql="select id,item,times from voteitems";
-
try {
-
con=getConnection();
-
items=new ArrayList<VoteItems>();
-
pstmt=con.prepareStatement(sql);
-
rs=pstmt.executeQuery();
-
while(rs.next()){
-
VoteItems item=new VoteItems();
-
item.setId(rs.getInt(1));
-
item.setItem(rs.getString(2));
-
item.setTimes(rs.getInt(3));
-
items.add(item);
-
}
-
} catch (ClassNotFoundException e) {
-
e.printStackTrace();
-
} catch (SQLException e) {
-
e.printStackTrace();
-
}finally{
-
try {
-
closeAll(rs, pstmt, con);
-
} catch (SQLException e) {
-
// TODO Auto-generated catch block
-
e.printStackTrace();
-
}
-
}
-
-
return items;
-
}
-
-
public boolean update(int id) {
-
String sql = "update voteitems set times = times + 1 where id = "+id;
-
Connection con = null;
-
PreparedStatement pstmt = null;
-
int n = -1;
-
-
try {
-
con = getConnection();
-
pstmt = con.prepareStatement(sql);
-
n=pstmt.executeUpdate();
-
} catch (ClassNotFoundException e) {
-
// TODO Auto-generated catch block
-
e.printStackTrace();
-
} catch (SQLException e) {
-
// TODO Auto-generated catch block
-
e.printStackTrace();
-
}finally{
-
try {
-
closeAll(null, pstmt, con);
-
} catch (SQLException e) {
-
// TODO Auto-generated catch block
-
e.printStackTrace();
-
}
-
}
-
return n>0;
-
}
-
-
}
-
package com.javaweb.servlet;
-
-
import java.io.IOException;
-
-
import javax.servlet.ServletException;
-
import javax.servlet.http.HttpServlet;
-
import javax.servlet.http.HttpServletRequest;
-
import javax.servlet.http.HttpServletResponse;
-
-
import com.javaweb.dao.VoteItemsDao;
-
import com.javaweb.dao.impl.VoteItemsDaoImpl;
-
-
public class UpdateVoteServlet extends HttpServlet {
-
-
/**
-
* The doGet method of the servlet. <br>
-
*
-
* This method is called when a form has its tag value method equals to get.
-
*
-
* @param request the request send by the client to the server
-
* @param response the response send by the server to the client
-
* @throws ServletException if an error occurred
-
* @throws IOException if an error occurred
-
*/
-
public void doGet(HttpServletRequest request, HttpServletResponse response)
-
throws ServletException, IOException {
-
doPost(request, response);
-
}
-
-
/**
-
* The doPost method of the servlet. <br>
-
*
-
* This method is called when a form has its tag value method equals to post.
-
*
-
* @param request the request send by the client to the server
-
* @param response the response send by the server to the client
-
* @throws ServletException if an error occurred
-
* @throws IOException if an error occurred
-
*/
-
public void doPost(HttpServletRequest request, HttpServletResponse response)
-
throws ServletException, IOException {
-
String[] strItems = request.getParameterValues("items");
-
-
-
-
VoteItemsDao dao = new VoteItemsDaoImpl();
-
-
for(String item:strItems){
-
dao.update(Integer.parseInt(item));
-
System.out.println(item);
-
}
-
//
-
response.sendRedirect("ShowVoteServlet");
-
//request.getRequestDispatcher("ShowVoteServlet").forward(request, response);
-
-
}
-
-
}
-
package com.javaweb.servlet;
-
-
-
import java.io.IOException;
-
import java.util.List;
-
-
-
import javax.servlet.ServletException;
-
import javax.servlet.http.HttpServlet;
-
import javax.servlet.http.HttpServletRequest;
-
import javax.servlet.http.HttpServletResponse;
-
-
-
import com.javaweb.dao.VoteItemsDao;
-
import com.javaweb.dao.impl.VoteItemsDaoImpl;
-
import com.javaweb.entity.VoteItems;
-
-
-
public class ShowVoteServlet extends HttpServlet {
-
-
-
/**
-
* The doGet method of the servlet. <br>
-
*
-
* This method is called when a form has its tag value method equals to get.
-
*
-
* @param request the request send by the client to the server
-
* @param response the response send by the server to the client
-
* @throws ServletException if an error occurred
-
* @throws IOException if an error occurred
-
*/
-
public void doGet(HttpServletRequest request, HttpServletResponse response)
-
throws ServletException, IOException {
-
-
-
doPost(request, response);
-
}
-
-
-
/**
-
* The doPost method of the servlet. <br>
-
*
-
* This method is called when a form has its tag value method equals to post.
-
*
-
* @param request the request send by the client to the server
-
* @param response the response send by the server to the client
-
* @throws ServletException if an error occurred
-
* @throws IOException if an error occurred
-
*/
-
public void doPost(HttpServletRequest request, HttpServletResponse response)
-
throws ServletException, IOException {
-
VoteItemsDao dao = new VoteItemsDaoImpl();
-
List<VoteItems> voteitems = dao.findAll();
-
request.setAttribute("voteitems", voteitems);
-
request.getRequestDispatcher("vote_result.jsp").
-
forward(request, response);
-
-
}
-
-
-
}
-
-
-
-
-
-
-
-
-
-
-
<%@ page language="java" contentType="text/html; charset=utf-8"%>
-
<%@ page import="java.util.*,com.javaweb.entity.*,com.javaweb.dao.*,com.javaweb.dao.impl.*" %>
-
<%
-
VoteItemsDao voteDao = new VoteItemsDaoImpl();
-
List<VoteItems> votes = voteDao.findAll();
-
-
%>
-
<html>
-
<head>
-
<title>调查问卷</title>
-
<style>
-
.outside{ /* 外层表格 */
-
background:url(bg1.jpg);
-
font-size:12px;
-
padding:0px;
-
}
-
-
.title{ /* 表格标题 */
-
color:#FFFFFF;
-
font-weight:bold;
-
text-align:center;
-
padding-top:3px;
-
padding-bottom:0px;
-
}
-
.tdoutside{
-
padding:0px 1px 4px 1px;
-
}
-
.inside{ /* 内层表格 */
-
width:269px;
-
font-size:12px;
-
padding:0px;
-
margin:0px;
-
}
-
.tdinside{
-
padding:7px 0px 7px 10px;
-
background-color:#FFD455;
-
}
-
form{
-
margin:0px; padding:0px;
-
}
-
input{
-
font-size:12px;
-
}
-
a{
-
color:#000000;
-
}
-
</style></head>
-
<body>
-
<table class="outside">
-
<tr><td class="title">点调查</td></tr>
-
<tr><td class="tdoutside">
-
<form method="post" action="UpdateVoteServlet">
-
<table class="inside" cellspacing="0">
-
<tr>
-
<td class="tdinside">
-
你对哪种技术最感兴趣<br>
-
<%
-
for(VoteItems v:votes){
-
%>
-
<input type="checkbox" name="items" value="<%=v.getId() %>"><%=v.getItem() %><br>
-
<%
-
-
}
-
%>
-
-
-
<input type="submit" value="提交">
-
<a href="ShowResultServlet">查看结果</a>
-
</td>
-
</tr>
-
</table>
-
</form>
-
</td></tr>
-
</table>
-
</body>
-
</html>
-
<%@ page language="java" import="java.util.*,com.javaweb.entity.*" pageEncoding="utf-8"%>
-
<%
-
String path = request.getContextPath();
-
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
-
-
List<VoteItems> voteitems =
-
(List<VoteItems>)request.getAttribute("voteitems");
-
-
%>
-
-
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
-
<html>
-
<head>
-
<base href="<%=basePath%>">
-
-
<title>My JSP 'vote_result.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>
-
<table>
-
<%
-
for(VoteItems v:voteitems){
-
%>
-
<tr>
-
<td><%=v.getItem() %></td>
-
<td><%=v.getTimes() %></td>
-
</tr>
-
<%} %>
-
</table>
-
</body>
-
</html>
文章来源: aaaedu.blog.csdn.net,作者:tea_year,版权归原作者所有,如需转载,请联系作者。
原文链接:aaaedu.blog.csdn.net/article/details/53138122
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)