|
今天写下两个小问题,都与数据库相关。第一个:前几天,实验室大迁移,学校网络也重新规划,Oracle的客户端设置也要随之改变,因为,服务器的IP地址换掉了(这也就是在很多情况下,连接服务器或者其他节点机器时,建议使用主机名的缘故——使用主机名,不会因为IP的更换而做相应的改变)。当然,重新安装肯定是一个方法,但是,毫无疑问,肯定有一个简单的设置即可完成,否则,Oracle就不叫“产品”了。正准备Google,见豪客在线,遂决定“站在豪客的肩膀上”,呵呵,豪客支招,一切迅速OK。修改Oracle客户端连接属性方法:进入:%OracleHome%/ora92/network/ADMIN,修改tnsnames.ora文件,将其中的HOST修改为新的IP即可。例如:BJMIGYJ =(DESCRIPTION =(ADDRESS_LIST =(ADDRESS = (PROTOCOL = TCP)(HOST = 10.17.158.133)(PORT = 1521)))(CONNECT_DATA =(SERVICE_NAME = bjmigyj)))第二个:需求很明确,一张日志表,有一字段为RequestTime(请求时间),需要以小时为单位取得请求数最多的时间段。同时,提供了模糊查询情况下的该统计功能,所谓模糊查询情况,就是可以限定某段时期,查询到该时期内的访问高峰。//构造查询条件:public String buildWhere(String beginTime,String endTime) {String strSql = "";if(!beginTime.toLowerCase().equals("")) {if(!endTime.toLowerCase().equals("")) {//起止时间都有strSql = strSql + " AND (requesttime between to_date('" + beginTime + "','yyyy-mm-dd hh24: mi: ss') and to_date('"+ endTime+ "','yyyy-mm-dd hh24: mi: ss'))";} else {//有开始时间,到当前系统日期为止strSql = strSql + " AND (requesttime >= to_date('" + beginTime + "','yyyy-mm-dd hh24: mi: ss'))";}} else//无开始时间{if(!endTime.toLowerCase().equals("")) {//到当前时间为止strSql = strSql + " AND (requesttime <= to_date('" + endTime + "','yyyy-mm-dd hh24: mi: ss'))";}}return strSql;}//获取请求集中的时间段:返回时间+数量public String getHighestHour(String where) throws SQLException {DbConnectionPool db = new DbConnectionPool();ResultSet rs=null;String strSql = "select to_char(requesttime,'yyyy-mm-dd hh24') requesttime,count(*) total from audit_extranet_log where 1=1 ";strSql += where;strSql = strSql + " group by to_char(requesttime,'yyyy-mm-dd hh24') order by count(*) desc";String time = "";int count = 0;try {System.out.println(strSql);rs=db.executeQuery(strSql);if(rs.next()){time = rs.getString("requesttime");count = rs.getInt("total");}} catch (Exception e) {e.printStackTrace();}finally{rs.close();db.closeConn();}return time + "时之后的一个小时内,请求数为:" +count ;}具体的JSP就不再说明,通过上述两个方法,配合页面参数,最后组合的查询类似如下(下面的情况只输入了开始时间):select to_char(requesttime,'yyyy-mm-dd-hh24') requesttime,count(*)from audit_admin_log where requesttime > to_date('2007-04-06 16:25:15','yyyy-mm-dd hh24: mi: ss')group by to_char(requesttime,'yyyy-mm-dd-hh24') order by count(*) desc; |
|
|