java代碼獲取數(shù)據(jù)庫(kù)表里數(shù)據(jù)的總數(shù)操作
在訪問(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)建、銷毀、增加,修改,刪除等動(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)建類
實(shí)現(xiàn)指定的監(jiān)聽(tīng)器接口中的方法
在web.xml文件中配置監(jiān)聽(tīng)/在類上標(biāo)注@WebListener 注解
第一類:域?qū)ο蟊O(jiān)聽(tīng)器
監(jiān)聽(tīng)域?qū)ο?創(chuàng)建與銷毀的監(jiān)聽(tīng)器
監(jiān)聽(tīng)器接口 描述 ServletContextListener 監(jiān)聽(tīng)Servlet上下文對(duì)象的創(chuàng)建、銷毀 HttpSessionListener 監(jiān)聽(tīng)會(huì)話對(duì)象的創(chuàng)建、銷毀 ServletRequestListener 監(jiān)聽(tīng)請(qǐng)求對(duì)象的創(chuàng)建、銷毀Servlet上下文對(duì)象 創(chuàng)建和銷毀的監(jiān)聽(tīng)器
public class ApplicationListener implements ServletContextListener {//Servlet上下文對(duì)象創(chuàng)建的時(shí)候被調(diào)用@Overridepublic 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() {@Overridepublic void run() {//System.out.println('發(fā)郵件....'+new Date());}}, 5000, 2000);}//Servlet上下文對(duì)象銷毀的時(shí)候被調(diào)用@Overridepublic void contextDestroyed(ServletContextEvent contextEvent) {System.out.println('Servlet上下文對(duì)象被銷毀啦...');//服務(wù)器在停止的時(shí)候,要執(zhí)行某些動(dòng)作,那么就可以把代碼寫在這個(gè)位置!!!}}
<!-- web.xml中配置 --><listener><listener-class>com.dream.listener.ApplicationListener</listener-class></listener>
會(huì)話對(duì)象 創(chuàng)建和銷毀的監(jiān)聽(tīng)器
@WebListenerpublic 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ì)象銷毀啦....'+session.getId()); }}
請(qǐng)求對(duì)象的創(chuàng)建和銷毀的監(jiān)聽(tīng)器
@WebListenerpublic 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ì)象的銷毀....'+request); }}
案例:統(tǒng)計(jì)網(wǎng)站在線人數(shù)
@WebListenerpublic 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) { }}@WebListenerpublic 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); }}
第二類:屬性監(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)器
@WebListenerpublic 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); }}
第三類:監(jiān)聽(tīng)HttpSession中的對(duì)象(JavaBean)
前兩類監(jiān)聽(tīng)器是作用在 ServletContext HttpSession ServletRequest上
第三類監(jiān)聽(tīng)器是作用在JavaBean上的。
注意:這類監(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ǔ)硬盤的過(guò)程
活化(反序列化):從硬盤中通過(guò)Session取出JavaBean對(duì)象到內(nèi)存的過(guò)程
javabean對(duì)象將要隨Session對(duì)象被鈍化(序列化)之前,web服務(wù)器調(diào)用該對(duì)象的
void sessionWillPassivate(HttpSessionEvent event) 方法
這樣javabean對(duì)象就可以知道自己將要和Session對(duì)象一起被鈍化到硬盤中
javabean對(duì)象將要隨Session對(duì)象被活化(反序列化)之后,web服務(wù)器調(diào)用該對(duì)象的void sessionDidActive(HttpSessionEvent event)方法
這樣javabean對(duì)象就可以知道自己將要和Session對(duì)象一起被活化回到內(nèi)存中
注意: 想要隨著Session 被鈍化、活化的對(duì)象它的類必須實(shí)現(xiàn)Serializable 接口,放在
Session中沒(méi)有實(shí)現(xiàn)Serilizable接口的對(duì)象,在Session鈍化時(shí),不會(huì)被序列化到磁盤上。
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ì)象被活化......'); }}
在WebContentMETA-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)有銷毀) 會(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ì)象它的類必須實(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è)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. IntelliJ IDEA設(shè)置自動(dòng)提示功能快捷鍵的方法2. 從Python的字符串中剝離所有非數(shù)字字符(“。”除外)3. ASP.NET MVC使用jQuery ui的progressbar實(shí)現(xiàn)進(jìn)度條4. android H5本地緩存加載優(yōu)化的實(shí)戰(zhàn)5. PHP程序員簡(jiǎn)單的開(kāi)展服務(wù)治理架構(gòu)操作詳解(二)6. IDEA一鍵完成格式化、去除無(wú)用引用、編譯的操作7. Python3 json模塊之編碼解碼方法講解8. 詳解如何使用Net將HTML簡(jiǎn)歷導(dǎo)出為PDF格式9. SpringBoot整合Redis的步驟10. PHP如何開(kāi)啟Opcache功能提升程序處理效率

網(wǎng)公網(wǎng)安備