Java programming and java programming
IP address counter
Principle: Obtain the user's IP address and store it in the database. When you access the database again, check whether the data exists in the database.
Design Process
Create a database connection class: 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 "; // load the database Driver static {try {Class through the constructor. forName (dbDriver ). newInstance ();} catch (Exception ex) {System. out. println ("Database loading failed") ;}// create Create a database Connection 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) {getS Tm (); try {rs00000000.exe cuteQuery (SQL);} catch (Exception e) {e. printStackTrace ();} return rs;} public int dosql (String SQL) {System. out. println (SQL); int I =-1; getStm (); try {i00000000.exe cuteUpdate (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 ();}}}
Create a core operation class 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 of the page accessed by the user
<% @ 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 (); %>
This program uses the mysql database and needs to import the mysql driver package
Database Table Structure
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;
You can also add a new function for this program.
Calculate the total number of IP addresses and the number of visits per IP address.
Instance 2
The database connection code is as follows:
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(); }}
Interface code 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 (); %>