首页
Web开发
Windows程序
编程语言
数据库
移动开发
系统相关
微信
其他好文
会员
首页
>
数据库
> 详细
【使用jdbc和servlet实现阅览文章访问次数】
时间:
2015-06-10 15:35:44
阅读:
143
评论:
0
收藏:
0
[点我收藏+]
标签:
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
踩
(
0
)
赞
(
0
)
举报
评论
一句话评论(
0
)
登录后才能评论!
分享档案
更多>
2021年07月29日 (22)
2021年07月28日 (40)
2021年07月27日 (32)
2021年07月26日 (79)
2021年07月23日 (29)
2021年07月22日 (30)
2021年07月21日 (42)
2021年07月20日 (16)
2021年07月19日 (90)
2021年07月16日 (35)
周排行
更多
数据库进阶
2021-07-29
在 Oracle 数据库中执行 SQL 语句遇到特殊字符的转义方式
2021-07-28
Windows Logstash同步 Sqlserver 到Elasticsearch
2021-07-26
mysql数据库(11):恢复数据
2021-07-26
mysql数据库(9):常用查询的例子
2021-07-26
SQLAlchemy 多对多
2021-07-26
ClickHouse的JDBC连接
2021-07-26
Apache HBase 1.7.1 发布,分布式数据库
2021-07-26
数据库常用架构和同步工作原理
2021-07-26
MySQL数据库设计规范(仅供参考)
2021-07-26
友情链接
兰亭集智
国之画
百度统计
站长统计
阿里云
chrome插件
新版天听网
关于我们
-
联系我们
-
留言反馈
© 2014
mamicode.com
版权所有 联系我们:gaon5@hotmail.com
迷上了代码!