您当前的位置: 首页 > 

JSP MVC模式案例之投票系统

发布时间:2016-11-12 09:08:39 ,浏览量:0

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 ListfindAll();
	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 ListfindAll() {
		Connection con = null;
		PreparedStatement pstmt = null;
		ResultSet rs=null;
		Listitems=null;
		String sql="select id,item,times from voteitems";
		try {
			con=getConnection();
			items=new ArrayList();
			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. 

	 *
	 * 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. 

	 *
	 * 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. 

	 *
	 * 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. 

	 *
	 * 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();
		Listvoteitems = 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();
	Listvotes = voteDao.findAll();
	
%>调查问卷
		
			
			
				
				
					
					
						点调查
					

				

				
					
					
						
						
							
							
								
								
									
									
										你对哪种技术最感兴趣

				<%  
					for(VoteItems v:votes){
						%><%=v.getItem() %>

						<%
					
					}
				 %>查看结果 
									

								

							

						

					

				

			

		
投票系统结果
<%@ 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+"/";

	Listvoteitems = 
		(List)request.getAttribute("voteitems");

%>

My JSP 'vote_result.jsp' starting page<%
    		for(VoteItems v:voteitems){
    	 %><%} %>
		
			
			
				
				
					
					
						<%=v.getItem() %>
					

					
						<%=v.getTimes() %>
					

				

			

		
关注
打赏
1688896170
查看更多评论

暂无认证

  • 0浏览

    0关注

    115984博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文
立即登录/注册

微信扫码登录

0.0481s