019.獲取每天該專案建立了多少個HttpSession物件
阿新 • • 發佈:2021-09-01
package com.item.utils; import com.alibaba.druid.pool.DruidDataSourceFactory; import javax.sql.DataSource; import java.io.IOException; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Properties; /** * Druid連線池的工具類 */public class JDBCUtils { //1.定義成員變數 DataSource private static DataSource ds; static { try { //1.載入配置檔案 Properties pro = new Properties(); pro.load(JDBCUtils.class.getClassLoader().getResourceAsStream("com/item/properties/druid.properties"));//2.獲取DataSource ds = DruidDataSourceFactory.createDataSource(pro); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } /** * 獲取連線 */ public static Connection getConnection() throwsSQLException { return ds.getConnection(); } /** * 釋放資源 */ public static void close(Statement stmt, Connection conn) { close(null, stmt, conn); } public static void close(ResultSet rs, Statement stmt, Connection conn) { if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } if (stmt != null) { try { stmt.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn != null) { try { conn.close();//歸還連線 } catch (SQLException e) { e.printStackTrace(); } } } /** * 獲取連線池(資料來源)方法 */ public static DataSource getDataSource() { return ds; } }
package com.item.utils; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; import java.sql.Timestamp; import java.util.Calendar; public class UpfateDate { public static PreparedStatement preparedStatement = null; public static Connection connection = null; /** * 1.向資料庫中寫入資料 */ public static void updataOrInsert(int onlineCounter) throws SQLException { connection = JDBCUtils.getConnection(); String sql = "insert into onlinecounter (onlineCounter,`time`) values (?,?)".trim(); preparedStatement = connection.prepareStatement(sql); preparedStatement.setInt(1, onlineCounter); preparedStatement.setTimestamp(2, new Timestamp(System.currentTimeMillis())); int i = preparedStatement.executeUpdate(); if (i > 0) { System.out.println("插入資料Successful!!!"); } else { System.out.println("插入資料Fail!!!"); } JDBCUtils.close(preparedStatement, connection); } }
package com.item.listener; import cn.hutool.core.date.DateTime; import cn.hutool.core.date.DateUnit; import cn.hutool.core.date.DateUtil; import com.item.utils.UpfateDate; import javax.servlet.annotation.WebListener; import javax.servlet.http.HttpSessionEvent; import javax.servlet.http.HttpSessionListener; import java.sql.SQLException; import java.util.Calendar; @WebListener public class OnlineCounterListener implements HttpSessionListener { //定義一個計數的變數,只要有HttpSession建立就會+1 public static int onlineCounter = 0; public static DateTime dateOld = DateUtil.date(); @Override public void sessionCreated(HttpSessionEvent se) { /** * 1.獲取日期的時間查 * 2.獲取明天 * 3.獲取今天 *4.建立一個全域性變數來儲存日期 */ try { DateTime dateNew = DateUtil.date();//永遠獲取當前新的日期 long betweenDay = DateUtil.between(dateOld, dateNew, DateUnit.DAY); if (betweenDay == 0) { UpfateDate.updataOrInsert(onlineCounter++); } else { onlineCounter=0; dateOld=dateNew; UpfateDate.updataOrInsert(onlineCounter++); } } catch (Exception e) { e.printStackTrace(); } } @Override public void sessionDestroyed(HttpSessionEvent se) { } }