使用jdbc和servlet实现阅览文章访问次数

package com.lm.org;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

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


/**
 * Servlet implementation class Test
 */
public class Test extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     */
    public Test() {
        super();
    }

	/**
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
		
	}

	/**
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		
		//HttpSession session = request.getSession();// 得到session
		//ServletContext application = super.getServletContext();
		//Integer accessCount = (Integer) application.getAttribute("accessCount");//数据库查询出来
		
		Integer wiewsCount=null;
		Connection connection = null;
		String sql="select * from t_news where id=1 ";//wiewsCount
		PreparedStatement ps=null;
		ResultSet rs=null;
		try {
			  connection=getConnection();
			  ps = connection.prepareStatement(sql);
			  rs = ps.executeQuery();
			  if(rs.next()){
				    wiewsCount = rs.getInt("wiewsCount");
				  if(wiewsCount==null){
					  wiewsCount = 1;
				  }else{
					  wiewsCount = wiewsCount + 1;
					  Connection cn =getConnection();
					  String sql1="update t_news set wiewsCount="+wiewsCount+" where id=1";
					  PreparedStatement prepareStatement = cn.prepareStatement(sql1);
					  int executeUpdate = prepareStatement.executeUpdate();
					  if(executeUpdate>0){
						  System.out.println("1");
					  }else{
						  System.out.println(2);
					  }
					  cn.close();
					  cn=null;
					  prepareStatement.close();
					  prepareStatement=null;
				  }
			  }
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			try {
				connection.close();
				connection=null;
				ps.close();
				ps=null;
				rs.close();
				rs=null;
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
		
		Integer ac;
		try {
			ac = accessCount(request, response);
			out.println("模拟值="+ac+"\t数据库值="+wiewsCount);
		} catch (Exception e) {
			e.printStackTrace();
		}
	
		out.flush();
		out.close();
	
	}

	
	public static Integer accessCount(HttpServletRequest request,HttpServletResponse response)throws Exception{
		HttpServletRequest hs=(HttpServletRequest)request;
		ServletContext application = hs.getServletContext();
		Integer accessCount = (Integer) application.getAttribute("accessCount");//数据库查询出来
		
		//if=null
		if (accessCount == null) {
			accessCount = 1;
			application.setAttribute("accessCount", accessCount);
		} else {
			//else +1并修改数据库的值
			accessCount = accessCount + 1;
			application.setAttribute("accessCount", accessCount);
			//update
		}
		return accessCount;
		
	}
	
	/**
	 * @return
	 */
	public static Connection getConnection() {
		Connection conn = null;
		try {
			Class.forName(Driver);
			conn = DriverManager.getConnection(Url, UserName, PassWord);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return conn;
	}
	private static String Driver = "com.mysql.jdbc.Driver";// ConfigUtils.getProperty("jdbc.driver");
	private static String Url = "jdbc:mysql://localhost:3306/test?characterEncoding=utf8";// ConfigUtils.getProperty("jdbc.url");
	private static String UserName = "root";// ConfigUtils.getProperty("jdbc.userName");
	private static String PassWord = "root";// ConfigUtils.getProperty("jdbc.passWord");

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值