【使用jdbc和servlet实现阅览文章访问次数】
Java代码
- 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");
- }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。