原理:獲取用戶的IP地址,然后存入數據庫,當再次訪問時查詢數據庫是否存在該條數據,即可完成此程序
設計過程
創建一個連接數據庫類:DB.java
package com.count.Online;import java.sql.*;public class DB { PRivate Connection con; private Statement stm; private ResultSet rs; private final static String url = "jdbc:MySQL://localhost:3306/oumyye"; private final static String dbDriver = "com.mysql.jdbc.Driver"; // 通過構造方法加載數據庫驅動 static { try { Class.forName(dbDriver).newInstance(); } catch (Exception ex) { System.out.println("數據庫加載失敗"); } } // 創建數據庫連接 public Connection getCon() { try { con = DriverManager.getConnection(url,"root","root"); System.out.println(con); con.setAutoCommit(true); } catch (SQLException e) { System.out.println(e.getMessage()); System.out.println("creatConnectionError!"); } return con; } public Statement getStm(){ try{ con=getCon(); stm=con.createStatement(); }catch(Exception e){e.printStackTrace(System.err);} return stm; } public Statement getStmed(){ try{ con=getCon(); stm=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY); }catch(Exception e){e.printStackTrace(System.err);} return stm; } public ResultSet search(String sql){ getStm(); try{ rs=stm.executeQuery(sql); }catch(Exception e){e.printStackTrace();} return rs; } public int dosql(String sql){ System.out.println(sql); int i=-1; getStm(); try{ i=stm.executeUpdate(sql); }catch(Exception e){e.printStackTrace();} return i; } public void closed(){ try{ if(rs!=null)rs.close(); } catch(Exception e){e.printStackTrace();} try{ if(stm!=null)stm.close(); } catch(Exception e){e.printStackTrace();} try{ if(con!=null)con.close(); } catch(Exception e){e.printStackTrace();} }}創建一個核心操作類CountOnline.java
package com.count.Online;import java.sql.*;public class CountOnline { private String userip; private String nowdate; private int times; private DB db=new DB(); public CountOnline(){} public void setUserip(String userip){ this.userip=userip; } public String getUserip(){ return this.userip; } public void setNowdate(String nowdate){ this.nowdate=nowdate; } public String getNowdate(){ return this.nowdate; } public void setTimes(int times){ this.times=times; } public int getTimes(){ return this.times; } public ResultSet adduser(){ ResultSet rs=null; String sql="insert into tb_IPcount values("+this.times+",'"+this.userip+"','"+this.nowdate+"')"; try{ db.dosql(sql); rs=db.search("select * from tb_IPcount"); }catch(Exception e){e.printStackTrace();} return rs; } public void dbclose(){ db.closed(); }}用戶訪問的頁面index.jsp
<%@ page contentType="text/html;charset=GBK"%><%@ page import="java.util.Date,java.text.*,java.sql.*" %><jsp:useBean id="mycount" class="com.count.Online.CountOnline"/><jsp:useBean id="mydb" class="com.count.Online.DB"/><% SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); String sql="select MAX(user_order) from tb_IPcount as max"; ResultSet rs=mydb.search(sql); rs.next(); int max=rs.getInt(1); mydb.closed(); mycount.setTimes(max+1); String ip=request.getRemoteAddr(); mycount.setUserip(ip); String nowdate=format.format(new Date()); mycount.setNowdate(nowdate); rs=mycount.adduser();%><html> <head> <title>記錄用戶IP地址的計數器</title> <link rel="stylesheet" type="text/CSS" href="css/style.css"> </head> <body> <center> <table height="90" width="400" border="1" bordercolor="black" bordercolorlight="black" bordercolordark="white" cellspacing="0" style="margin-top:200"> <tr bgcolor="lightgrey"> <td align="center">第N位訪問者</td> <td align="center">訪問者IP地址</td> <td align="center">訪問時間</td> </tr> <% while(rs.next()){ %> <tr> <td align="center"><%=rs.getInt("user_order")%></td> <td align="center"><%=rs.getString("user_ip")%></td> <td align="center"><%=rs.getString("user_time")%></td> </tr> <% } mycount.dbclose(); %> <tr> <td align="center" colspan="3"> 您是第<%=max+1%>位訪問者! <br> 您的IP為:<%=ip%> <br> 您訪問的時間為:<%=nowdate%> </td> </tr> </table> </center> </body></html>本程序使用的是mysql數據庫,需導入mysql驅動包
數據庫表結構
CREATE TABLE `tb_ipcount` ( `user_order` int(10) DEFAULT NULL, `user_ip` varchar(20) DEFAULT NULL, `user_time` varchar(20) DEFAULT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;
對于此程序還可以添加一個新功能
計算出總共多少ip地址,每個ip地址的訪問次數。
實例二
數據庫連接代碼如上
CountOnline.java
package com.count.Online;import java.sql.*;public class CountOnline { private String userip; private String nowdate; private int times; private DB db=new DB(); public CountOnline(){} public void setUserip(String userip){ this.userip=userip; } public String getUserip(){ return this.userip; } public void setNowdate(String nowdate){ this.nowdate=nowdate; } public String getNowdate(){ return this.nowdate; } public void setTimes(int times){ this.times=times; } public int getTimes(){ return this.times; } public ResultSet checkuser(){ String sql="select * from tb_newusercount where user_ip='"+this.userip+"'"; ResultSet rs=null; try{ rs=db.search(sql); if(rs.next()){ this.times=rs.getInt("user_times")+1; sql="update tb_newusercount set user_times="+this.times+" where user_ip='"+this.userip+"'"; db.dosql(sql); } else{ this.times=1; sql="insert into tb_newusercount values('"+this.userip+"',1)"; db.dosql(sql); } rs=db.search("select * from tb_newusercount"); }catch(Exception e){e.printStackTrace();} return rs; } public void dbclose(){ db.closed(); }}界面代碼index.jsp
<%@ page contentType="text/html;charset=GBK"%><%@ page import="java.sql.*" %><jsp:useBean id="mycount" class="com.count.Online.CountOnline"/><% String ip=request.getRemoteAddr(); mycount.setUserip(ip); ResultSet rs=mycount.checkuser(); rs.last(); int num=rs.getRow();%><html> <head> <title>只對新用戶計數的計數器</title> <link rel="stylesheet" type="text/css" href="css/style.css"> </head> <body> <center> <table height="90" width="200" border="1" bordercolor="black" bordercolorlight="black" bordercolordark="white" cellspacing="0" style="margin-top:200"> <tr bgcolor="lightgrey"> <td align="center">訪問者IP地址</td> <td align="center">訪問次數</td> </tr> <% rs.beforeFirst(); while(rs.next()){ %> <tr> <td align="center"><%=rs.getString("user_ip")%></td> <td align="center"><%=rs.getInt("user_times")%></td> </tr> <% } %> <tr> <td align="center" colspan="2"> 您的IP為:<%=ip%> <br> 您的訪問次數為:<%=mycount.getTimes()%>次 <br> 共有 <%=num%> 個新用戶訪問過本頁 </td> </tr> </table> <% mycount.dbclose(); %> </center> </body></html>新聞熱點
疑難解答