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程序设计的更多相关文章

  1. 《Java程序设计》 课程教学

    <Java程序设计> 课程教学 给学生 考核方式 100分构成 翻转课堂考核12次(5*12 = 60):每次考试20-30道题目,考试成绩规格化成5分(比如总分20分就除以4) 注意:不 ...

  2. 2016-2017-2 《Java程序设计》教学进程

    2016-2017-2 <Java程序设计>教学进程 目录 考核方式 课前准备 教学进程 第00周学习任务和要求 第01周学习任务和要求 第02周学习任务和要求 第03周学习任务和要求 第 ...

  3. 2016-2017-2 《Java程序设计》预备作业2总结

    2016-2017-2 <Java程序设计>预备作业2总结 古希腊学者普罗塔戈说过:「头脑不是一个要被填满的容器,而是一束需要被点燃的火把.」 在对计算机系的学生情况的调查中,我说: 最近 ...

  4. 2016-2017-2 《Java程序设计》预备作业1 总结

    2016-2017-2 <Java程序设计>预备作业1 总结 预备作业01:你期望的师生关系是什么见https://edu.cnblogs.com/campus/besti/2016-20 ...

  5. 2016-2017-2 《Java程序设计》课程学生博客和代码托管链接

    2016-2017-2 <Java程序设计>课程学生博客和代码托管链接 博客 1552 20155201 李卓雯 20155202 张 旭 20155203 杜可欣 20155204 王 ...

  6. 《Java程序设计与数据结构教程(第二版)》学习指导

    <Java程序设计与数据结构教程(第二版)>学习指导 欢迎关注"rocedu"微信公众号(手机上长按二维码) 做中教,做中学,实践中共同进步! 原文地址:http:// ...

  7. 20145208 《Java程序设计》第0周学习总结

    20145208 <Java程序设计>第0周学习总结 阅读心得 读了老师推荐的几个文章,虽然第四个文章"为什么一定要自学"报告资源不存在而无法阅读,其他的三篇文章都言之 ...

  8. # 2015-2016-2 《Java程序设计》课程总结

    2015-2016-2 <Java程序设计>课程总结

  9. 积极主动敲代码,使用Junit学习Java程序设计

    积极主动敲代码,使用JUnit学习Java 早起看到周筠老师在知乎的回答软件专业成绩很好但是实际能力很差怎么办?,很有感触. 从读大学算起,我敲过不下100本程序设计图书的代码,我的学习经验带来我的程 ...

  10. 20145205《Java程序设计》课程总结

    每周读书笔记链接汇总 20145205 <Java程序设计>第1周学习总结 20145205<Java程序设计>第2周学习总结 20145205 <Java程序设计> ...

随机推荐

  1. 《JAVA与模式》之备忘录模式

    一.备忘录(Memento)模式结构 备忘录对象是一个用来存储另外一个对象内部状态的快照(snapshot)的对象.备忘录模式的用意是在不破坏封装的条件下,将一个对象的状态捕捉住,并外部化,存储起来, ...

  2. 转 linux进程内存到底怎么看 剖析top命令显示的VIRT RES SHR值

    引 言: top命令作为Linux下最常用的性能分析工具之一,可以监控.收集进程的CPU.IO.内存使用情况.比如我们可以通过top命令获得一个进程使用了多少虚拟内存(VIRT).物理内存(RES). ...

  3. 【转】Kotlin 和 Checked Exception

    Kotlin 和 Checked Exception 最近 JetBrains 的 Kotlin 语言忽然成了热门话题.国内小编们传言说,Kotlin 取代了 Java,成为了 Android 的“钦 ...

  4. Groovy 学习手册(5)

    8. 函数式编程 函数式编程(FP)是一种编程风格,侧重于函数和最小化状态的变化(使用不可变的数据结构).它更接近于用数学来表达解决方案,而不是循序渐进的操作. 在函数式编程里,其功能应该是" ...

  5. 容错处理库Polly使用文档

    Design For Failure1. 一个依赖服务的故障不会严重破坏用户的体验.2. 系统能自动或半自动处理故障,具备自我恢复能力. 以下是一些经验的服务容错模式 超时与重试(Timeout an ...

  6. Python 爬虫实例(14) 爬取 百度音乐

    #-*-coding:utf-8-*- from common.contest import * import urllib def spider(): song_types = ['新歌','热歌' ...

  7. Vue基本概念介绍及vue-cli环境搭建

    1 js中初始化一个Vue对象,传的参数就是对象属性. 挂载点.模板.实例之间的关系. var vm = new Vue({ el:"#app", template:'<di ...

  8. 【OfficeWebViewer】在线预览Word,Excel~

    今天有个需求, 直接支持web端预览word,excel等文件, 查了一下很多写的比较麻烦, 这里找到一种简单的方式: http://view.officeapps.live.com/op/view. ...

  9. logstash匹配多行日志

    在工作中,遇到一个问题就是日志的处理,首选的方案就是ELFK(filebeat+logstash+es+kibana) 因为之前使用过logstash采集日志的时候,非常的消耗系统的资源,所以这里我选 ...

  10. java.util.concurrent.ExecutionException: com.android.ide.common.process.ProcessException 的解决办法之一

    在查看别人的代码的时候,遇到了把工程导入到 Android Studio ,报:ExecutionException: com.android.ide.common.process.ProcessEx ...