package com.test;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.NumberFormat;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Digg extends HttpServlet {
	private static Connection con = null;
	private static Statement stmt = null;

	/**
	 * Constructor of the object.
	 */
	public Digg() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * 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 {

		this.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 {
		request.setCharacterEncoding("utf8");
		response.setCharacterEncoding("utf8");
        String action = request.getParameter("action");
        String digtype = request.getParameter("digtype");
        if(action.equals("digs")){
        	try {
        		response.getWriter().write(dig(digtype));
				
			} catch (Exception e) {
				e.printStackTrace();
			}
        }else if(action.equals("getdigshtml")){
        	try {
        		response.getWriter().write(getDigHtml());
			} catch (Exception e) {
				e.printStackTrace();
			}
        }
	}
   private String dig(String digtype)throws Exception{
	   String sql ="";
	   if(digtype.equals("digs")){
		   sql ="update dig set digs=digs+1 where id =1";
	   }else{
		   sql ="update dig set undigs=undigs+1 where id =1";
	   }
	   int num =stmt.executeUpdate(sql);
	   if(num>0){
		   return "3";
	   }
	   return "1";
   }
   public static void main(String[] args){ 
	   NumberFormat nf = NumberFormat.getPercentInstance(); 
	   nf.setMaximumIntegerDigits(4); 
	    nf.setMaximumFractionDigits(6); 
	   double d = (double)1/(double)7; 
	   System.out.println(nf.format(d)); 
	   } 
   private String getDigHtml()throws Exception{
	   NumberFormat nf = NumberFormat.getPercentInstance(); 
	   nf.setMaximumIntegerDigits(3); 
	   nf.setMaximumFractionDigits(2); 
	   
	   String sql ="select * from dig where id=1";
	   ResultSet res = stmt.executeQuery(sql);
	   double digSum = 0 ;
	   double unDigSum =0 ;
	   double digSumAll = 0;
	   String digPer = "0%";
	   String unDigPer = "0%";
	   while(res.next()){
		   digSum = res.getInt("digs");
		   unDigSum = res.getInt("undigs");
	   }
	   digSumAll = digSum + unDigSum;
	   if(digSumAll !=0 ){
		   digPer = nf.format(digSum/digSumAll);
		   unDigPer = nf.format(unDigSum/digSumAll);
	   }
	   
	   String str="<div class='good'>";
			str+="<a href=JavaScript:isdigs('digs') >";
			str+="<p>很好</p><div class='bar'><div id='g_img' style='width:"+digPer+"'></div></div>";
			str+="<span class='num'>"+digPer+"("+digSum+")</span>";
			str+="</a></div><div class='bad'>";
			str+="<a href=JavaScript:isdigs('undigs') >";
			str+="<p>很差</p><div class='bar'><div id='b_img' style='width:"+unDigPer+"'></div></div>";
			str+="<span class='num'>"+unDigPer+"("+unDigSum+")</span>";
			str+="</a></div>";
	
	   return str;
	   
   }
	/**
	 * Initialization of the servlet. <br>
	 * 
	 * @throws ServletException
	 *             if an error occurs
	 */
	public void init() throws ServletException {
		try {
			Class.forName("com.mysql.jdbc.Driver");
			con = DriverManager.getConnection(
					"jdbc:mysql://127.0.0.1:3306/dig", "root", "root");
			stmt = con.createStatement();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public void closeCon() {
		try {
			stmt.close();
			con.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
}
最近下载更多
能不能不存在  LV13 2020年4月21日
javaboy001  LV2 2018年6月10日
tZuyuuuu  LV2 2018年1月16日
232妮妮  LV12 2017年10月21日
2361545535  LV9 2017年10月17日
酷睿科技  LV11 2017年10月11日
jhpjlfd  LV1 2017年9月23日
jjdjjh  LV1 2017年7月27日
hun3474  LV2 2016年10月31日
changzhoufen  LV2 2016年6月13日
最近浏览更多
微信网友_6698455742353408 2023年10月19日
暂无贡献等级
pangxiangdong  LV4 2023年3月12日
malike  LV3 2022年12月24日
喃喵xxxx  LV6 2022年8月16日
ifif14  LV2 2022年5月11日
644800782  LV4 2022年3月1日
xpc 2021年11月8日
暂无贡献等级
嘉辉  LV2 2021年9月12日
LL68888 2021年8月3日
暂无贡献等级
410043877  LV2 2021年7月8日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友