教大家使用java实现顶一下踩一下功能

网友投稿 293 2023-02-17


教大家使用java实现顶一下踩一下功能

本文实例为大家分享了java实现顶一下踩一下功能的具体代码,供大家参考,具体内容如下

效果图如下:

主页面index.html:

后台servlet:

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.

*/

public void destroy() {

super.destroy(); // Just puts "destroy" string in log

// Put your code here

}

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

this.doPost(request, response);

}

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 = Numberhttp://Format.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="

str+="";

str+="

很好

str+=""+digPer+"("+digSum+")";

str+="

str+="";

str+="

很差

str+=""+unDigPer+"("+unDigSum+")";

str+="

return str;

}

/**

* Initialization of the servlet.

*

* @throws ServletException

* if an error occurs

*/

public void init() throws ServletException {

try {

Class.forName("com.mysql.jdbc.Driver");

con = DriverManager.getConnection(

"jdbc:mysql://172.16.42.39:3306/dig", "root", "12345678");

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();

}

}

}

sql语句:

CREATE TABLE dig(

id INT PRIMARY KEY,

digs INT,

undigs INT

);

INSERT INTO dig VALUES(1,0,0);


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:怎样看api接口文档(api接口在哪里打开)
下一篇:Java+opencv3.2.0之直方图均衡详解
相关文章

 发表评论

暂时没有评论,来抢沙发吧~