"Use JDBC and servlet for viewing article visits"

Source: Internet
Author: User

Java code

    1. 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 ();//Get session
    • ServletContext application = Super.getservletcontext ();
    • Integer accesscount = (integer) application.getattribute ("Accesscount");//Database query out
  • 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 ("Analog value =" +ac+ "\ t Database value =" +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");//Database query out
  • If=null
  • if (Accesscount = = null) {
  • Accesscount = 1;
  • Application.setattribute ("Accesscount", Accesscount);
  • } else {
  • else +1 and modify the values of the database
  • 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");
  • }

"Use JDBC and servlet for viewing article visits"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.