码迷,mamicode.com
首页 > 编程语言 > 详细

java程序设计

时间:2015-01-31 23:09:08      阅读:296      评论:0      收藏:0      [点我收藏+]

标签:

IP地址计数器

原理:获取用户的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>

java程序设计

标签:

原文地址:http://www.cnblogs.com/oumyye/p/4264860.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!