日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2024/10/12 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【使用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");??
    • ??
    • } ?

    轉載于:https://www.cnblogs.com/techfox/p/4566125.html

    總結

    以上是生活随笔為你收集整理的【使用jdbc和servlet实现阅览文章访问次数】的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。