java代码获取数据库表里数据的总数操作
在访问数据库时,特别是新手,可能会需要查询表中数据总数,以下这段代码可以非常简便的获取到数据数目
//先建立数据库连接,执行查询语句 Connectionconn=DriverManager.getConnection(URL,USER,PassWord); Statementst=conn.createStatement(); ResultSetrs=st.executeQuery("selectcount(*)asresultfromtablename"); //创建变量存取个数 intcount=0; while(rs.next()) { count=getInt(1); }
补充知识:JavaWeb之Listener监听器及Session的钝化与活化
概念
监听器用于监听web应用中某些对象、信息的创建、销毁、增加,修改,删除等动作的
发生,然后作出相应的响应处理。当范围对象的状态发生变化的时候,服务器自动调用
监听器对象中的方法。
常用于统计在线人数和在线用户,系统加载时进行信息初始化,统计网站的访问量等。
创建步骤
创建类
实现指定的监听器接口中的方法
在web.xml文件中配置监听/在类上标注@WebListener注解
第一类:域对象监听器
监听域对象创建与销毁的监听器
监听器接口 | 描述 |
---|---|
ServletContextListener | 监听Servlet上下文对象的创建、销毁 |
HttpSessionListener | 监听会话对象的创建、销毁 |
ServletRequestListener | 监听请求对象的创建、销毁 |
Servlet上下文对象创建和销毁的监听器
publicclassApplicationListenerimplementsServletContextListener{ //Servlet上下文对象创建的时候被调用 @Override publicvoidcontextInitialized(ServletContextEventcontextEvent){ System.out.println("Servlet上下文对象被创建啦..."); //项目一旦启动,此处代码运行! Timertimer=newTimer(); //5秒钟之后开始执行,以后每间隔2秒发送一封邮件! timer.schedule(newTimerTask(){ @Override publicvoidrun(){ //System.out.println("发邮件...."+newDate()); } },5000,2000); } //Servlet上下文对象销毁的时候被调用 @Override publicvoidcontextDestroyed(ServletContextEventcontextEvent){ System.out.println("Servlet上下文对象被销毁啦..."); //服务器在停止的时候,要执行某些动作,那么就可以把代码写在这个位置!!! } }
com.dream.listener.ApplicationListener
会话对象创建和销毁的监听器
@WebListener publicclassSessionListenerimplementsHttpSessionListener{ @Override publicvoidsessionCreated(HttpSessionEventevent){ HttpSessionsession=event.getSession(); System.out.println("session对象创建啦...."+session.getId()); } @Override publicvoidsessionDestroyed(HttpSessionEventevent){ HttpSessionsession=event.getSession(); System.out.println("session对象销毁啦...."+session.getId()); } }
请求对象的创建和销毁的监听器
@WebListener publicclassRequestListenerimplementsServletRequestListener{ @Override publicvoidrequestInitialized(ServletRequestEventevent){ ServletRequestrequest=event.getServletRequest(); System.out.println("Request对象的创建...."+request); } @Override publicvoidrequestDestroyed(ServletRequestEventevent){ ServletRequestrequest=event.getServletRequest(); System.out.println("Request对象的销毁...."+request); } }
案例:统计网站在线人数
@WebListener publicclassApplicationListenerimplementsServletContextListener{ @Override publicvoidcontextInitialized(ServletContextEventevent){ //项目启动,向application对象中存一个变量,初始值0 ServletContextapplication=event.getServletContext(); application.setAttribute("count",0); } @Override publicvoidcontextDestroyed(ServletContextEventevent){ } } @WebListener publicclassSessionListenerimplementsHttpSessionListener{ @Override publicvoidsessionCreated(HttpSessionEventevent){ //有人访问了count++ HttpSessionsession=event.getSession(); ServletContextapplication=session.getServletContext(); intcount=(Integer)application.getAttribute("count"); count++; application.setAttribute("count",count); } @Override publicvoidsessionDestroyed(HttpSessionEventevent){ //有人离开了count-- HttpSessionsession=event.getSession(); ServletContextapplication=session.getServletContext(); Integercount=(Integer)application.getAttribute("count"); count--; application.setAttribute("count",count); } }
第二类:属性监听器
监听域对象属性变化的监听器
监听器接口 | 描述 |
---|---|
ServletContextAttributeListener | 监听Servlet上下文对象属性的创建、删除、替换 |
HttpSessionAttributeListener | 监听会话对象属性的创建、删除、替换 |
ServletRequestAttributeListener | 监听请求对象属性的创建、删除、替换 |
Servlet上下文对象属性变化的监听器
@WebListener publicclassApplicationAttributeListenerimplementsServletContextAttributeListener{ //Servlet上下文对象新增值的时候被调用 @Override publicvoidattributeAdded(ServletContextAttributeEventevent){ Stringstr="Servlet上下文对象中添加了属性:"+event.getName() +",属性值是:"+event.getValue(); System.out.println(str); } //Servlet上下文对象删除值的时候被调用 @Override publicvoidattributeRemoved(ServletContextAttributeEventevent){ Stringstr="Servlet上下文对象中删除了属性:"+event.getName() +",属性值是:"+event.getValue(); System.out.println(str); } //Servlet上下文对象替换值的时候被调用 @Override publicvoidattributeReplaced(ServletContextAttributeEventevent){ Stringstr="Servlet上下文对象中替换了属性:"+event.getName() +",属性值是:"+event.getValue(); System.out.println(str); } }
第三类:监听HttpSession中的对象(JavaBean)
前两类监听器是作用在ServletContextHttpSessionServletRequest上
第三类监听器是作用在JavaBean上的。
注意:这类监听器不需要在web.xml中配置
监听器接口 | 描述 |
---|---|
HttpSessionBindingListener | 监听会话对象中JavaBean对象的绑定、删除 |
HttpSessionActivationListener | 监听会话对象中JavaBean对象的钝化、活化 |
会话对象中JavaBean对象的绑定和删除的监听器
实现了HttpSessionBindingListener接口的JavaBean对象可以感知自己被绑定到Session中和Session中删除的事件
当对象被绑定到HttpSession对象中时,web服务器调用该对象的
voidvalueBound(HttpSessionBindingEventevent)方法
当对象从HttpSession对象中解除绑定时,web服务器调用该对象的
voidvalueUnbound(HttpSessionBindingEventevent)方法
publicclassUserimplementsHttpSessionBindingListener{ privateintid; privateStringname; publicUser(){ } publicUser(intid,Stringname){ this.id=id; this.name=name; } publicintgetId(){ returnid; } publicvoidsetId(intid){ this.id=id; } publicStringgetName(){ returnname; } publicvoidsetName(Stringname){ this.name=name; } publicvoidvalueBound(HttpSessionBindingEventevent){ System.out.println("对象绑定到了Session中"); } publicvoidvalueUnbound(HttpSessionBindingEventevent){ System.out.println("对象从Session中移除"); } }
<%@pageimport="com.dream.vo.User"%> <%@pagelanguage="java"pageEncoding="UTF-8"%>ServletContextAttributeListener监听器测试 <% Useruser=newUser(1,"aaa"); session.setAttribute("user",user); session.removeAttribute("user"); %>
会话对象中JavaBean对象的钝化和活化的监听器
实现了HttpSessionActivationListener接口的JavaBean对象可以感知自己被活化(反序列化)和钝化(序列化)的事件
钝化(序列化):在内存中JavaBean对象通过Session存储硬盘的过程
活化(反序列化):从硬盘中通过Session取出JavaBean对象到内存的过程
javabean对象将要随Session对象被钝化(序列化)之前,web服务器调用该对象的
voidsessionWillPassivate(HttpSessionEventevent)方法
这样javabean对象就可以知道自己将要和Session对象一起被钝化到硬盘中
javabean对象将要随Session对象被活化(反序列化)之后,web服务器调用该对象的voidsessionDidActive(HttpSessionEventevent)方法
这样javabean对象就可以知道自己将要和Session对象一起被活化回到内存中
注意:想要随着Session被钝化、活化的对象它的类必须实现Serializable接口,放在
Session中没有实现Serilizable接口的对象,在Session钝化时,不会被序列化到磁盘上。
publicclassUserimplementsSerializable,HttpSessionActivationListener{ privatestaticfinallongserialVersionUID=-1566395353697458460L; privateintid; privateStringname; publicUser(){ } publicUser(intid,Stringname){ this.id=id; this.name=name; } publicintgetId(){ returnid; } publicvoidsetId(intid){ this.id=id; } publicStringgetName(){ returnname; } publicvoidsetName(Stringname){ this.name=name; } //钝化 @Override publicvoidsessionWillPassivate(HttpSessionEventevent){ System.out.println("对象被钝化......."+event.getSource()); } //活化 @Override publicvoidsessionDidActivate(HttpSessionEventevent){ System.out.println("对象被活化......"); } }
在WebContent\META-INF文件夹下创建一个context.xml文件
面试题:Session的钝化与活化
钝化:当服务器正常关闭时,还存活着的session(在设置时间内没有销毁)会随着服务
器的关闭被以文件(“SESSIONS.ser”)的形式存储在tomcat的work目录下,这个过程叫
做Session的钝化。
活化:当服务器再次正常开启时,服务器会找到之前的“SESSIONS.ser”文件,从中恢
复之前保存起来的Session对象,这个过程叫做Session的活化。
注意事项
想要随着Session被钝化、活化的对象它的类必须实现Serializable接口,还有的是只有在服务器正常关闭的条件下,还未超时的Session才会被钝化成文件。当Session超时、调用invalidate方法或者服务器在非正常情况下关闭时,Session都不会被钝化,因此也就不存在活化。
在被钝化成“SESSIONS.ser”文件时,不会因为超过Session过期时间而消失,这个文件会一直存在,等到下一次服务器开启时消失。
当多个Session被钝化时,这些被钝化的Session都被保存在一个文件中,并不会为每个Session都建立一个文件。
以上这篇java代码获取数据库表里数据的总数操作就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。