JSP Hit Counter Free Open Code

posted by Scudyen date: Thursday, May 22, 2008 category: JSP
  Featured Story | Printable View | Email It | Comments | Write Comment

 

This counter uses MS SQL Server to store hits. I have used a custom stored procedure to add and display hit.  This is a very good example using stored procedures with JSP.

You may use this script only with MS SQL. First you should create a table and a procedure. You may use Query Manager.

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[counter]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[counter]
GO

CREATE TABLE [dbo].[counter] (
 [hit] [int] NOT NULL
) ON [PRIMARY]
GO
INSERT [dbo].[counter] (hit) VALUES (1)
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[webcounter]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[webcounter]
GO

SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO

CREATE PROCEDURE dbo.webcounter
AS
BEGIN
SET NOCOUNT ON
DECLARE @hits INT
update counter set hit = hit + 1
SELECT hit from counter
END
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO

Now you can create our script. You may put this code anywhere you want to display the counter :

<%@ page import="java.sql.*" %>
<%
  Connection ocon = null;
  Class.forName("com.inet.tds.TdsDriver").newInstance();
  ocon = DriverManager.getConnection("jdbc:inetdae7:localhost", "sa", "");
  ocon.setCatalog( "dotnet" );

  Statement stmtt = ocon.createStatement();
  ResultSet rc = stmtt.executeQuery("EXEC dbo.webcounter");
  rc.next();
  int pages = rc.getInt(1);
  out.println("Hits : " + pages + "<br>");
%>


I have used i-net MERLIA JDBC driver.  



Custom Search



Gazatem Technologies Active News Manager


page counter

Listed with 411asp.net
Listed with 4aspin.com
PHP, ASP, .NET, JSP Resources, Reviews