欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java代碼獲取數(shù)據(jù)庫(kù)表里數(shù)據(jù)的總數(shù)操作

 更新時(shí)間:2020年08月15日 14:54:47   作者:可樂(lè)opp  
這篇文章主要介紹了java代碼獲取數(shù)據(jù)庫(kù)表里數(shù)據(jù)的總數(shù)操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧

在訪問(wèn)數(shù)據(jù)庫(kù)時(shí),特別是新手,可能會(huì)需要查詢表中數(shù)據(jù)總數(shù),以下這段代碼可以非常簡(jiǎn)便的獲取到數(shù)據(jù)數(shù)目

//先建立數(shù)據(jù)庫(kù)連接,執(zhí)行查詢語(yǔ)句
Connection conn = DriverManager.getConnection(URL, USER, PassWord);
Statement st=conn.createStatement();
ResultSet rs =st.executeQuery("select count(*) as result from tablename");
//創(chuàng)建變量存取個(gè)數(shù)
int count=0;
while(rs.next())
{
count=getInt(1);
}

補(bǔ)充知識(shí):JavaWeb 之 Listener監(jiān)聽(tīng)器及Session的鈍化與活化

概念

監(jiān)聽(tīng)器用于監(jiān)聽(tīng)web應(yīng)用中某些對(duì)象、信息的創(chuàng)建、銷(xiāo)毀、增加,修改,刪除等動(dòng)作的

發(fā)生,然后作出相應(yīng)的響應(yīng)處理。當(dāng)范圍對(duì)象的狀態(tài)發(fā)生變化的時(shí)候,服務(wù)器自動(dòng)調(diào)用

監(jiān)聽(tīng)器對(duì)象中的方法。

常用于統(tǒng)計(jì)在線人數(shù)和在線用戶,系統(tǒng)加載時(shí)進(jìn)行信息初始化,統(tǒng)計(jì)網(wǎng)站的訪問(wèn)量等。

創(chuàng)建步驟

創(chuàng)建類(lèi)

實(shí)現(xiàn)指定的監(jiān)聽(tīng)器接口中的方法

在web.xml文件中配置監(jiān)聽(tīng)/在類(lèi)上標(biāo)注@WebListener 注解

第一類(lèi):域?qū)ο蟊O(jiān)聽(tīng)器

監(jiān)聽(tīng)域?qū)ο?創(chuàng)建與銷(xiāo)毀的監(jiān)聽(tīng)器

監(jiān)聽(tīng)器接口 描述
ServletContextListener 監(jiān)聽(tīng)Servlet上下文對(duì)象的創(chuàng)建、銷(xiāo)毀
HttpSessionListener 監(jiān)聽(tīng)會(huì)話對(duì)象的創(chuàng)建、銷(xiāo)毀
ServletRequestListener 監(jiān)聽(tīng)請(qǐng)求對(duì)象的創(chuàng)建、銷(xiāo)毀

Servlet上下文對(duì)象 創(chuàng)建和銷(xiāo)毀的監(jiān)聽(tīng)器

public class ApplicationListener implements ServletContextListener {	
	//Servlet上下文對(duì)象創(chuàng)建的時(shí)候被調(diào)用
	@Override
	public void contextInitialized(ServletContextEvent contextEvent) {
		System.out.println("Servlet上下文對(duì)象被創(chuàng)建啦...");
		
    //項(xiàng)目一旦啟動(dòng),此處代碼運(yùn)行!
		Timer timer=new Timer();
		//5秒鐘之后開(kāi)始執(zhí)行,以后每間隔2秒發(fā)送一封郵件!
		timer.schedule(new TimerTask() {
			@Override
			public void run() {
				//System.out.println("發(fā)郵件...."+new Date());
			}
		}, 5000, 2000);
	}
	//Servlet上下文對(duì)象銷(xiāo)毀的時(shí)候被調(diào)用
	@Override
	public void contextDestroyed(ServletContextEvent contextEvent) {
		System.out.println("Servlet上下文對(duì)象被銷(xiāo)毀啦...");
		//服務(wù)器在停止的時(shí)候,要執(zhí)行某些動(dòng)作,那么就可以把代碼寫(xiě)在這個(gè)位置?。?!	
	}
}
<!-- web.xml中配置 -->
<listener>
	<listener-class>com.dream.listener.ApplicationListener</listener-class>
</listener>

會(huì)話對(duì)象 創(chuàng)建和銷(xiāo)毀的監(jiān)聽(tīng)器

@WebListener
public class SessionListener implements HttpSessionListener{
 @Override
 public void sessionCreated(HttpSessionEvent event) {
 HttpSession session = event.getSession();
 System.out.println("session對(duì)象創(chuàng)建啦...."+session.getId());
 }
 @Override
 public void sessionDestroyed(HttpSessionEvent event) {
 HttpSession session = event.getSession();
 System.out.println("session對(duì)象銷(xiāo)毀啦...."+session.getId());
 }
}

請(qǐng)求對(duì)象的創(chuàng)建和銷(xiāo)毀的監(jiān)聽(tīng)器

@WebListener
public class RequestListener implements ServletRequestListener{

 @Override
 public void requestInitialized(ServletRequestEvent event) {
 ServletRequest request = event.getServletRequest();
 System.out.println("Request對(duì)象的創(chuàng)建...."+request);
 }
 @Override
 public void requestDestroyed(ServletRequestEvent event) {
 ServletRequest request = event.getServletRequest();
 System.out.println("Request對(duì)象的銷(xiāo)毀...."+request);
 }

}

案例:統(tǒng)計(jì)網(wǎng)站在線人數(shù)

@WebListener
public class ApplicationListener implements ServletContextListener{
 @Override
 public void contextInitialized(ServletContextEvent event) {
 //項(xiàng)目啟動(dòng),向application對(duì)象中存一個(gè)變量,初始值0
 ServletContext application = event.getServletContext(); 
 application.setAttribute("count", 0);
 }
 @Override
 public void contextDestroyed(ServletContextEvent event) {
 }
}

@WebListener
public class SessionListener implements HttpSessionListener {

 @Override
 public void sessionCreated(HttpSessionEvent event) {
 // 有人訪問(wèn)了 count++
 HttpSession session = event.getSession();
 ServletContext application = session.getServletContext();

 int count =(Integer) application.getAttribute("count");
 count++;
 application.setAttribute("count", count);
 }
 @Override
 public void sessionDestroyed(HttpSessionEvent event) {
 // 有人離開(kāi)了 count--
 HttpSession session = event.getSession();
 ServletContext application = session.getServletContext();
 
 Integer count =(Integer) application.getAttribute("count");
 count--;
 application.setAttribute("count", count);
 }
}

第二類(lèi):屬性監(jiān)聽(tīng)器

監(jiān)聽(tīng)域?qū)ο髮傩宰兓谋O(jiān)聽(tīng)器

監(jiān)聽(tīng)器接口 描述
ServletContextAttributeListener 監(jiān)聽(tīng)Servlet上下文對(duì)象屬性的創(chuàng)建、刪除、替換
HttpSessionAttributeListener 監(jiān)聽(tīng)會(huì)話對(duì)象屬性的創(chuàng)建、刪除、替換
ServletRequestAttributeListener 監(jiān)聽(tīng)請(qǐng)求對(duì)象屬性的創(chuàng)建、刪除、替換

Servlet上下文對(duì)象屬性變化的監(jiān)聽(tīng)器

@WebListener
public class ApplicationAttributeListener implements ServletContextAttributeListener{

  //Servlet上下文對(duì)象新增值的時(shí)候被調(diào)用
 @Override
 public void attributeAdded(ServletContextAttributeEvent event) {
 String str = "Servlet上下文對(duì)象中添加了屬性:"+event.getName()
      +",屬性值是:"+event.getValue();
 System.out.println(str);
 }
 //Servlet上下文對(duì)象刪除值的時(shí)候被調(diào)用
 @Override
 public void attributeRemoved(ServletContextAttributeEvent event) {
 String str = "Servlet上下文對(duì)象中刪除了屬性:"+event.getName()
      +",屬性值是:"+event.getValue();
 System.out.println(str);
 }
 //Servlet上下文對(duì)象替換值的時(shí)候被調(diào)用
 @Override
 public void attributeReplaced(ServletContextAttributeEvent event) {
 String str = "Servlet上下文對(duì)象中替換了屬性:"+event.getName()
      +",屬性值是:"+event.getValue();
 System.out.println(str);
 }
}

第三類(lèi):監(jiān)聽(tīng)HttpSession中的對(duì)象(JavaBean)

前兩類(lèi)監(jiān)聽(tīng)器是作用在 ServletContext HttpSession ServletRequest上

第三類(lèi)監(jiān)聽(tīng)器是作用在JavaBean上的。

注意:這類(lèi)監(jiān)聽(tīng)器不需要在web.xml中配置

監(jiān)聽(tīng)器接口 描述
HttpSessionBindingListener 監(jiān)聽(tīng)會(huì)話對(duì)象中JavaBean對(duì)象的綁定、刪除
HttpSessionActivationListener 監(jiān)聽(tīng)會(huì)話對(duì)象中JavaBean對(duì)象的鈍化、活化

會(huì)話對(duì)象中JavaBean對(duì)象的綁定和刪除的監(jiān)聽(tīng)器

實(shí)現(xiàn)了HttpSessionBindingListener接口的JavaBean對(duì)象可以感知自己被綁定到Session中和 Session中刪除的事件

當(dāng)對(duì)象被綁定到HttpSession對(duì)象中時(shí),web服務(wù)器調(diào)用該對(duì)象的

void valueBound(HttpSessionBindingEvent event)方法

當(dāng)對(duì)象從HttpSession對(duì)象中解除綁定時(shí),web服務(wù)器調(diào)用該對(duì)象的

void valueUnbound(HttpSessionBindingEvent event)方法

public class User implements HttpSessionBindingListener {
 private int id;
 private String name;

 public User() {
 }
 public User(int id, String name) {
 this.id = id;
 this.name = name;
 }
 public int getId() {
 return id;
 }
 public void setId(int id) {
 this.id = id;
 }
 public String getName() {
 return name;
 }
 public void setName(String name) {
 this.name = name;
 }
 public void valueBound(HttpSessionBindingEvent event) {
 System.out.println("對(duì)象綁定到了Session中");
 }
 public void valueUnbound(HttpSessionBindingEvent event) {
 System.out.println("對(duì)象從Session中移除");
 }
}
<%@ page import="com.dream.vo.User"%>
<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE HTML>
<html>
<head>
<title>ServletContextAttributeListener監(jiān)聽(tīng)器測(cè)試</title>
</head>
<body>
 <%
 User user = new User(1, "aaa");
 session.setAttribute("user", user);
 session.removeAttribute("user");
 %>
</body>
</html>

會(huì)話對(duì)象中JavaBean對(duì)象的鈍化和活化的監(jiān)聽(tīng)器

實(shí)現(xiàn)了HttpSessionActivationListener接口的JavaBean對(duì)象可以感知自己被活化(反序列化)和鈍化(序列化)的事件

鈍化(序列化):在內(nèi)存中JavaBean對(duì)象通過(guò)Session存儲(chǔ)硬盤(pán)的過(guò)程

活化(反序列化):從硬盤(pán)中通過(guò)Session取出JavaBean對(duì)象到內(nèi)存的過(guò)程

javabean對(duì)象將要隨Session對(duì)象被鈍化(序列化)之前,web服務(wù)器調(diào)用該對(duì)象的

void sessionWillPassivate(HttpSessionEvent event) 方法

這樣javabean對(duì)象就可以知道自己將要和Session對(duì)象一起被鈍化到硬盤(pán)中

javabean對(duì)象將要隨Session對(duì)象被活化(反序列化)之后,web服務(wù)器調(diào)用該對(duì)象的void sessionDidActive(HttpSessionEvent event)方法

這樣javabean對(duì)象就可以知道自己將要和Session對(duì)象一起被活化回到內(nèi)存中

注意: 想要隨著Session 被鈍化、活化的對(duì)象它的類(lèi)必須實(shí)現(xiàn)Serializable 接口,放在

Session中沒(méi)有實(shí)現(xiàn)Serilizable接口的對(duì)象,在Session鈍化時(shí),不會(huì)被序列化到磁盤(pán)上。

public class User implements Serializable, HttpSessionActivationListener{
 private static final long serialVersionUID = -1566395353697458460L;
 private int id;
 private String name;
 public User() {
 }
 public User(int id, String name) {
 this.id = id;
 this.name = name;
 }
 public int getId() {
 return id;
 }
 public void setId(int id) {
 this.id = id;
 }
 public String getName() {
 return name;
 }
 public void setName(String name) {
 this.name = name;
 }
 //鈍化
 @Override
 public void sessionWillPassivate(HttpSessionEvent event) {
 System.out.println("對(duì)象被鈍化......." + event.getSource());
 }
 //活化
 @Override
 public void sessionDidActivate(HttpSessionEvent event) {
 System.out.println("對(duì)象被活化......");
 }
}

在WebContent\META-INF文件夾下創(chuàng)建一個(gè)context.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<Context>
 <!-- 
 maxIdleSwap:"1": session如果1分鐘沒(méi)有使用就序列化
 directory: 序列化后文件所保存的路徑 
 -->
 <Manager className="org.apache.catalina.session.PersistentManager"
 maxIdleSwap="1">
 <Store className="org.apache.catalina.session.FileStore"  
  directory="C:\\text" />
 </Manager>
</Context> 

面試題:Session 的鈍化與活化

鈍化:當(dāng)服務(wù)器正常關(guān)閉時(shí),還存活著的session(在設(shè)置時(shí)間內(nèi)沒(méi)有銷(xiāo)毀) 會(huì)隨著服務(wù)

器的關(guān)閉被以文件(“SESSIONS.ser”)的形式存儲(chǔ)在tomcat 的work 目錄下,這個(gè)過(guò)程叫

做Session 的鈍化。

活化:當(dāng)服務(wù)器再次正常開(kāi)啟時(shí),服務(wù)器會(huì)找到之前的“SESSIONS.ser” 文件,從中恢

復(fù)之前保存起來(lái)的Session 對(duì)象,這個(gè)過(guò)程叫做Session的活化。

注意事項(xiàng)

想要隨著Session 被鈍化、活化的對(duì)象它的類(lèi)必須實(shí)現(xiàn)Serializable 接口,還有的是只有在服務(wù)器正常關(guān)閉的條件下,還未超時(shí)的Session 才會(huì)被鈍化成文件。當(dāng)Session 超時(shí)、調(diào)用invalidate方法或者服務(wù)器在非正常情況下關(guān)閉時(shí),Session 都不會(huì)被鈍化,因此也就不存在活化。

在被鈍化成“SESSIONS.ser” 文件時(shí),不會(huì)因?yàn)槌^(guò)Session 過(guò)期時(shí)間而消失,這個(gè)文件會(huì)一直存在,等到下一次服務(wù)器開(kāi)啟時(shí)消失。

當(dāng)多個(gè)Session 被鈍化時(shí),這些被鈍化的Session 都被保存在一個(gè)文件中,并不會(huì)為每個(gè)Session 都建立一個(gè)文件。

以上這篇java代碼獲取數(shù)據(jù)庫(kù)表里數(shù)據(jù)的總數(shù)操作就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Spring?Security如何實(shí)現(xiàn)升級(jí)密碼加密方式詳解

    Spring?Security如何實(shí)現(xiàn)升級(jí)密碼加密方式詳解

    這篇文章主要為大家介紹了Spring?Security實(shí)現(xiàn)升級(jí)密碼加密方式詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-01-01
  • Java編程生產(chǎn)者消費(fèi)者實(shí)現(xiàn)的四種方法

    Java編程生產(chǎn)者消費(fèi)者實(shí)現(xiàn)的四種方法

    Java生產(chǎn)者和消費(fèi)者問(wèn)題是線程安全模型中的經(jīng)典問(wèn)題:生產(chǎn)者和消費(fèi)者在同一個(gè)時(shí)間段共用同一個(gè)存儲(chǔ)空間,生產(chǎn)者向存儲(chǔ)空間中添加產(chǎn)品呢,消費(fèi)者取走產(chǎn)品,當(dāng)存儲(chǔ)空間為空時(shí),消費(fèi)者阻塞,當(dāng)存儲(chǔ)空間滿時(shí),生產(chǎn)者阻塞
    2021-10-10
  • 使用ResponseEntity處理API返回問(wèn)題

    使用ResponseEntity處理API返回問(wèn)題

    這篇文章主要介紹了使用ResponseEntity處理API返回問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-07-07
  • java基礎(chǔ)之Object類(lèi)

    java基礎(chǔ)之Object類(lèi)

    這篇文章主要介紹了java基礎(chǔ)之Object類(lèi) 的相關(guān)資料,需要的朋友可以參考下
    2015-06-06
  • java中的String定義的字面量最大長(zhǎng)度是多少

    java中的String定義的字面量最大長(zhǎng)度是多少

    這篇文章主要介紹了java中的String定義的字面量最大長(zhǎng)度是多少,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-12-12
  • Spring3.1.1+MyBatis3.1.1的增、刪、查、改以及分頁(yè)和事務(wù)管理

    Spring3.1.1+MyBatis3.1.1的增、刪、查、改以及分頁(yè)和事務(wù)管理

    這篇文章主要介紹了Spring3.1.1+MyBatis3.1.1的增、刪、查、改以及分頁(yè)和事務(wù)管理的相關(guān)資料,需要的朋友可以參考下
    2016-01-01
  • spring結(jié)合struts的代碼詳解

    spring結(jié)合struts的代碼詳解

    這篇文章主要介紹了spring結(jié)合struts的代碼詳解,需要的朋友可以參考下
    2017-09-09
  • Swift洗牌動(dòng)畫(huà)效果的實(shí)現(xiàn)方法

    Swift洗牌動(dòng)畫(huà)效果的實(shí)現(xiàn)方法

    這篇文章主要介紹了Swift洗牌動(dòng)畫(huà)效果的實(shí)現(xiàn)方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧
    2016-12-12
  • 徹底理解Java 中的ThreadLocal

    徹底理解Java 中的ThreadLocal

    這篇文章主要介紹了徹底理解Java 中的ThreadLocal的相關(guān)資料,需要的朋友可以參考下
    2017-07-07
  • springboot集成mybatis-plus遇到的問(wèn)題及解決方法

    springboot集成mybatis-plus遇到的問(wèn)題及解決方法

    這篇文章主要介紹了springboot集成mybatis-plus遇到的問(wèn)題及解決方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-11-11

最新評(píng)論