【使用jdbc和servlet实现阅览文章访问次数】
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了【使用jdbc和servlet实现阅览文章访问次数】,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含5239字,纯文字阅读大概需要8分钟。
内容图文
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");
- }
【使用jdbc和servlet实现阅览文章访问次数】
标签:
本文系统来源:http://www.cnblogs.com/techfox/p/4566125.html
内容总结
以上是互联网集市为您收集整理的【使用jdbc和servlet实现阅览文章访问次数】全部内容,希望文章能够帮你解决【使用jdbc和servlet实现阅览文章访问次数】所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。