The count of simple statistics

  Abstract: simple count of Statistics 

  </ Td> </ tr> <tr> <td height="35" valign="top" class="ArticleTeitle"> <table width="738" border="0"> <tr> <td width = " 732 "> </ td> </ tr> </ table> 
  JSP recent study.    Read a book, read others in the online writing some simple counter, his hands began to write one, and now share.    Feeling can be for beginners, it should be learning is a good example, designed to JAVABEAN, SESSION connecting to the database and some simple question, but for beginners, it can deepen the understanding of these concepts. 
  I do simple statistics, the main anti-updated record of visiting IP, IP number of these simple statistical functions first to design databases.    I have a simple ACCESS (mainly the easy-to-use), the first two in the ACCESS in the design table: 1, counter table (including 2: Number, counter) - This table is used to record the number of visits. 
  2, iptables table (including 2: Number, IP) - This table is used to record the IP address of visitors.    Well, good database design, as long as the following on your machine can be registered under the ODBC Rafah, I registered here ODBC data source is counter 
  Well, I am now the procedure code stickers out: 
  / / CounterBean.java 
  Package yg; 
  Import java.io. *; 
  Import java.sql .*; 

  Public class CounterBean extends Object ( 
  String sDBDriver = "sun.jdbc.odbc.JdbcOdbcDriver"; 
  String sConnStr = "jdbc: odbc: count"; 
  Private Connection conn = null; 
  Private Statement stmt = null; 
  ResultSet rs = null; 
  / / Registration database-driven procedures 
  Public CounterBean () ( 
  Try ( 
  Class.forName (sDBDriver); 
  ) 
  Catch (java.lang.ClassNotFoundException e) ( 
  System.err.println ( "CounterBean ():" + e.getMessage ()); 
  ) 
  ) 

  / / Definition of the establishment of a database connectivity and data query 
  Public ResultSet executeQuery (String sql) ( 
  Rs = null; 
  Try ( 
  Conn = DriverManager.getConnection (sConnStr); 
  Stmt = conn.createStatement (); 
  Rs = stmt.executeQuery (sql); 
  ) 
  Catch (SQLException e) ( 
  System.err.println ( "executeQuery:" + e.getMessage ()); 
  ) 
  Return rs; 
  ) 

  / / Definition of data manipulation 
  Public void executeUpdate (String sql) ( 
  Stmt = null; 
  Rs = null; 
  Try ( 
  Conn = DriverManager.getConnection (sConnStr); 
  Stmt = conn.createStatement (); 
  Stmt.executeUpdate (sql); 
  Stmt.close (); 
  Conn.close (); 
  ) 
  Catch (SQLException ex) ( 
  System.err.println ( "executeUpdate:" + ex.getMessage ()); 
  ) 
  ) 

  / / Database closed 
  Public void closeStmt () ( 
  Try ( 
  Stmt.close (); 
  ) 
  Catch (SQLException e) ( 
  E.printStackTrace (); 
  ) 
  ) 

  Public void closeConn () ( 
  Try ( 
  Conn.close (); 
  ) 
  Catch (SQLException e) ( 
  E.printStackTrace (); 
  ) 
  ) 
  / / Synchronous updated counter 
  Public synchronized int getcount () ( 
  Int count = 0; 
  Rs = null; 
  Try ( 
  ExecuteUpdate ( "update counter set counter = counter +1"); 
  Rs = executeQuery ( "select counter from counter"); 
  Rs.next (); 
  Count = rs.getInt (the "counter"); 
  Stmt.close (); 
  Conn.close (); 
  ) Catch (SQLException e) () 
  Return count; 
  ) 
  ) 


  / / Counter.jsp 
  <% @ Page contentType = "text / html; gb2312 charset ="%> 
  <% @ Page import = "yg.CounterBean"%> 
  <% @ Page import = "java.io. *"%> 
  <% @ Page import = "java.sql .*"%> 

<head>



  </ Head> 
<body>


<%
  String cont = new String (); 
  String ips = new String (); 
  Int ipnum = 0; 
  ResultSet rs; 
  Rs = null; 
  If (session.isNew ()) ( 
  Cont = String.valueOf (counter.getcount ());// from visits 
  Session.putValue ( "count", cont); 
  Ips = request.getRemoteAddr ();// visitor's IP 
  Rs = counter.executeQuery ( "select ip from iptables where ip = '" + ips +"'"); 
  If (! Rs.next ())// If there is no record of this IP database 
  Counter.executeUpdate ( "insert into iptables (ip) values ( '" + ips +"')");// insert a record of IP 
  ) 
  Rs = counter.executeQuery ( "select ip from iptables"); 
  While (rs.next ()){// statistics of IP 
  Ipnum ipnum = +1; 
  ) 
  Counter.closeStmt (); 
  Counter.closeConn (); 
  Session.putValue ( "ipnum" String.valueOf (ipnum)); 
%>
  You are the <% = session.getValue ( "count")%> visitors 


  Your IP address is: <% = request.getRemoteAddr ()%> 


  At present, there are <% = session.getValue ( "ipnum")%> different IP visited this site 


  </ Body> 

  Well, the end, these Rafah.    Duality is a rookie, a JAVA beginners, the guidance also looking to the guidance of prawns. 

  </ Td> </ tr> <tr> 

  ↑ Back 

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Facebook
  • DotNetKicks
  • DZone
  • Netvouz
  • Propeller

Recommend Articles

Comments

Leave a Reply