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. You've implemented -[<UIApplicationDelegate> application:didReceiveRemoteNotification:fetchCompletionHandler:], but you still need to add "remote-notification" to the list of your supported UIBackgrou

    最近有个同事问我,他工程运行时就会有如下提示,但是不影响功能:You've implemented -[<UIApplicationDelegate> application:didRec ...

  2. mysqldump: Couldn't execute 'SET OPTION SQL_QUOTE_SHOW_CREATE=1'

    源码安装的mysql数据库,在执行mysqldump的时候报错: # mysqldump -u root -p --all-databases > dbdump.db Enter passwor ...

  3. Visual C++中的TCHAR

    为了使代码兼容ASCII码和Unicode编码,微软公司还提供了通用字符类型TCHAR. 通用字符类型的含义是,假设在项目属性中选择"Unicode字符集".则TCHAR代表WCH ...

  4. UI自动化测试元素定位思想

    2014年的最后一天,以一篇短文纪念一下. 经常看到有同学说UI自动化测试定位难,找不到北.这话是不错的,定位是难,灵活且复杂,需要经验加技术,但是有写东西是可以提炼出来作为思想去推而广之的. 简单来 ...

  5. 带你开发一款给Apk中自己主动注入代码工具icodetools(开凿篇)

    一.前言 从这篇開始咋们開始一个全新的静态方式逆向工具icodetools的实现过程.这个也是我自己第一次写的个人认为比較实用的小工具,特别是在静态方式逆向apk找关键点的时候.兴许会分为三篇来具体介 ...

  6. Python小游戏、小程序

    python 小游戏之摇骰子猜大小 python 实现一个双色球生成程序 python-循环与判断练习题

  7. SqlExcel使用文档及源码

    昨天帮朋友做了个小工具,以完成多表连接处理一些数据.今天下班后又做了份使用文档,不知友能看懂否?现将使用文档及源码发布如下,以供有同样需求的朋友下载. 使用文档 一.增.改.查.删 1.增(向shee ...

  8. svn up 排除目录更新

    svn update --set-depth=exclude tmp 则可以排除tmp目录的更新

  9. unity, 2d rope

    https://www.youtube.com/watch?v=l6awvCT29yU

  10. RocketMQ最佳实践(一)4.0版本/概念介绍/安装调试/客户端demo

    为什么选择RocketMQ 我们来看看官方回答: “我们研究发现,对于ActiveMQ而言,随着越来越多的使用queues和topics,其IO成为了瓶颈.某些情况下,消费者缓慢(消费能力不足)还会拖 ...