zoukankan      html  css  js  c++  java
  • 基于javascript的asp数据库操作类,含分页、字符串截取、用户登陆验证[原创]

      2005.01.15发表于blog.csdn.net/zxub

      无聊又用了用asp,发现还是不爽,已经习惯了面向对象了,就想改进了,vbscript不用,感觉看起来很不爽,就用javascript写了下面个类:
    <%
    //////////////////////////////////////////////////
    //            //
    //    用Javascript实现的一个数据库操作类        //
    //               //
    //////////////////////////////////////////////////
     function DBOperate()
     {
      this.DBType=0;  //数据文件类别号,用以实现获取不同的表
            this.DBPath="";  //数据库文件相对站点的存放地址
      this.ConnString; //连接字符串
      this.Conn; //数据库连接对象
      this.RS;   //数据集
      this.SQLString;  //操作数据库的SQL语句
      this.GetDBPath=GetDBPath;  //获取数据库路径
      this.RSMoveType=1; //纪录集移动方式:
                       //0为只读,当前数据记录只能向下移动;
                       //1为只读,当前数据记录可自由移动;
                       //2为可读写,当前数据记录可自由移动;
                       //3为可读写,当前数据记录可自由移动,可看到新增记录
           //RS.MoveFirst(),RS.MoveLast(),RS.MoveNext(),RS.MovePrevious().
           //RS.Move(n) 方法:从当前位置移动n条记录,n>0为正向,n<0为反向,
           //RS.Move(n,1) 方法:从第一条纪录处移动n条记录,n>0,后面的参数只能为1
      this.RSLockType=1; //纪录集锁定方式:
                       //1为缺省锁定类型,记录集是只读的,不能修改记录
           //2为悲观锁定,当修改记录时,数据提供者将尝试锁定记录以确保成功地编辑记录。只要编辑一开始,则立即锁住记录。
                       //3为乐观锁定,直到用Update方法提交更新记录时才锁定记录。
                       //4为批量乐观锁定,允许修改多个记录,只有调用UpdateBatch方法后才锁定记录。
      //*************************************************************************
      //当分页的时候,计算TotalRecordCount和TotalPageCount
      //*************************************************************************
      this.TotalRecordCount=0;  //纪录集总纪录数,开始由于没纪录集,所以为0
      this.PageSize=10;    //每页最大纪录数,默认为10
      this.TotalPageCount=0;  //最大页数. 
      //*************************************************************************
      this.CurrentPageID=1;  //当前页码,默认为1
      this.CurrentPageTag="CurrentPageID";
      this.GotoPageName="";  //显示纪录的页面名称,默认为当前页面
      function GetDBPath()
      {
       switch (this.DBType)
       {
        case 1:
         this.DBPath="/data/#info.asp";
         break;
        case 2:
         this.DBPath="/data/#members.asp";
         break;
        case 3:
         this.DBPath="/data/#tradeinfo.asp";
         break;
        default:Response.Write("数据类别号有误!");Response.End;break;
       }
       if (this.DBPath!=null)
       {
        this.ConnString="Provider=microsoft.jet.oledb.4.0;data source="+Server.MapPath(this.DBPath);
       }
      }
      this.DBOpen=DBOpen;
      function DBOpen()  //打开数据库
      {
       try
       {
        this.Conn=Server.CreateObject("ADODB.Connection");
        this.Conn.Open(this.ConnString);
       }
       catch (e)
       {
        Response.Write("数据库连接错误!");
        Response.End();
       }   
      }
      this.DBClose=DBClose;
      function DBClose()  //关闭数据库
      {
       if (this.Conn!=null)
       {
        this.Conn.Close();
        this.Conn=null;
       }
      }
      this.GetRS=GetRS;
      function GetRS()  //产生一个数据集对象
      {
       if (this.Conn!=null)
       {
        var i=Request.QueryString(this.CurrentPageTag).Count;
        if (i>0)
        {
         this.CurrentPageID=parseInt(Request.QueryString(this.CurrentPageTag));
         if (this.CurrentPageID<=0)
         {
          this.DBClose();
          Response.Write("页码超出合法范围!");
          Response.End();
         }
        }
        this.RS=Server.CreateObject("ADODB.RecordSet");
        this.RS.Open(this.SQLString,this.Conn,this.RSMoveType,this.RSLockType);
        this.TotalRecordCount=this.RS.RecordCount;
        this.TotalPageCount=Math.ceil(this.TotalRecordCount/this.PageSize);
        var EndPage=this.TotalPageCount;
        if (EndPage==0) EndPage=1;
        if (this.CurrentPageID>EndPage)
        {
         this.DBClose();
         Response.Write("页码超出合法范围!");
         Response.End();
        }
       }    
       else
       {
        Response.Write("没有连接到数据库!");
        Response.End();
       }
      }
      this.CloseRS=CloseRS;
      function CloseRS() //关闭数据集对象
      {
       if (this.RS!=null)
       {
        this.RS.Close();
        this.RS=null;
       }
      }
      this.DBExecSQL=DBExecSQL;
      function DBExecSQL()  //执行SQL语句,用于执行添加、删除、修改操作
      {
       if (this.Conn!=null)
       {
        this.Conn.Execute(this.SQLString);
       }
       else
       {
        Response.Write("没有连接到数据库!");
        Response.End();
       }
      }
      this.RSIsEmpty=RSIsEmpty;
      function RSIsEmpty()
      {
       if (this.RS!=null)
       {
        if ((this.RS.BOF) && (this.RS.EOF))
        {
         //RS is empty
         return true;
        }
        else
        {
         //RS not empty
         return false;
        }
       }
       else
       {
        Response.Write("没有连接到数据库!");
        Response.End();
       }   
      }
      this.SetPager=SetPager;
      function SetPager()
      {
       var PreviousPageID=this.CurrentPageID-1;
       var NextPageID=this.CurrentPageID+1;
       var HomePage=1;
       var EndPage=this.TotalPageCount;
       //************************************************************
       /*this.RS.MoveFirst();
       this.RS.Move((this.CurrentPageID-1)*this.PageSize,1);
       var j=0;
       while (!this.RS.eof && j<this.PageSize)
       {
        Response.Write(this.RS("type")+"<br>");
        j=j+1;
        this.RS.MoveNext();
       }*/
       //************************************************************   
       var PagerString="";
       PagerString+="<table width=\"100%\" height=\"25\"  border=\"0\" cellpadding=\"0\" cellspacing=\"0\" style=\"font-size:9pt\">";
       PagerString+="<tr>";
       PagerString+="<td align=\"right\" valign=\"middle\">合计 <strong><font color=\"#FF0000\">";
       PagerString+=this.TotalRecordCount;
       PagerString+="</font></strong> 个 | ";
       if (this.CurrentPageID==HomePage)
       {
        PagerString+="<A disabled>首页</A> <A disabled>上一页</A> ";
       }
       else
       {
        PagerString+="<A href="+this.GotoPageName+"?"+this.CurrentPageTag+"="+HomePage+">首页</A> <A href="+this.GotoPageName+"?"+this.CurrentPageTag+"="+PreviousPageID+">上一页</A> ";
       }
       if (this.CurrentPageID==EndPage)
       {
        PagerString+="<A disabled>下一页</A> <A disabled>尾页</A> </td>";
       }
       else
       {
        PagerString+="<A href="+this.GotoPageName+"?"+this.CurrentPageTag+"="+NextPageID+">下一页</A> <A href="+this.GotoPageName+"?"+this.CurrentPageTag+"="+EndPage+">尾页</A> </td>";
       }
       PagerString+="<td width=\"120\" align=\"center\" valign=\"middle\">页次:<strong><font color=\"#FF0000\">";
       PagerString+=this.CurrentPageID;
       PagerString+="</font>/";
       PagerString+=this.TotalPageCount+"&nbsp;";
       PagerString+=this.PageSize+"</strong>个/页 </td>"
       PagerString+="<td width=\"36\" align=\"right\" valign=\"middle\">转到: </td>"
       PagerString+="<td width=\"76\" align=\"left\" valign=\"middle\">";
       PagerString+="<select size=1 name=CurrentPage style='font-size:9pt;60px' onchange=\"document.location=\'"+this.GotoPageName+"?"+this.CurrentPageTag+"=\'+this.options[this.selectedIndex].value\">";
       for (i=1;i<=this.TotalPageCount;i++)
       {
        if(this.CurrentPageID==i)
        {
         PagerString+="<option selected value="+i+">第"+i+"页</option>";
        }
        else
        {
         PagerString+="<option value="+i+">第"+i+"页</option>";
        }
       }
       PagerString+="</select>";
       PagerString+="</td>";
       PagerString+="</tr>";
       PagerString+="</table>" 
       if (EndPage>0)
       {
        return(PagerString);
       }
       else
       {
        return("");
       }
      }
     } 
    %>
    <%
    //////////////////////////////////////////////////
    //            //
    //    字符串操作                                //
    //               //
    //////////////////////////////////////////////////
       function CutString(InputString,AppendString,CutLen)
      {
       InputString=String(InputString);
       AppendString=String(AppendString);
       var FactLen=0,TempString="",i;
       var Strlength=InputString.length;
       for (i=0;i<Strlength;i++)
       {
        if (InputString.charCodeAt(i)>255)
        {
         FactLen+=2;
        }
        else
        {
         FactLen+=1;
        }    
        if (FactLen>CutLen)
         break;    
        try
        {
         TempString+=InputString.substr(i,1);
        }
        catch (e)
        {
         break;
        }    
       }
       if (FactLen>CutLen)
       {
        TempString+=AppendString;
       }
       return TempString;
      }  
    %>
    <%
    ///////////////////////////////////////////////////
    //
    //  空格及回车的显示                            //
    //
    //////////////////////////////////////////////////
      function AlignString(obj)
      {
       return String(obj).replace( /[\" "]/g,"&nbsp;").replace(/[\r]/g,"<br>");
      }
    %>
    <%
    //////////////////////////////////////////////////
    //            //
    //    用Javascript实现的用户身份验证类            //
    //               //
    //////////////////////////////////////////////////
     function UserLogin()
     {
      this.UnameField="";  //用户名字段名称
      this.PwdField="";  //密码字段名称
      this.LevelField="";  //用户级别字段名称
      this.UserTableName="";  //存放用户信息的表名称
      this.uname="";  //用户名
      this.pwd="";  //密码
      this.SessionOfName="";
      this.SessionOfPwd="";
      this.SessionOfLevel="";
      this.CheckLogin=CheckLogin;  //验证用户是否登陆
      this.ErrorURL="";  //没登陆时转向的页面
      this.OKURL="";  //登陆成功时转向的页面
      function CheckLogin()
      {
       var DBConn=new DBOperate();
       DBConn.DBType=0;
       DBConn.CurrentPageTag="#PID";
       DBConn.ConnString="Provider=microsoft.jet.oledb.4.0;data source="+Server.MapPath("/data/#members.asp");
       DBConn.DBOpen();
       DBConn.SQLString="Select * from "+this.UserTableName+" where "+this.UnameField+"='"+this.uname.replace( /[\']/g,"#").replace( /[\s]/g,"#")+"' and "+this.PwdField+"='"+this.pwd.replace( /[\']/g,"#").replace( /[\s]/g,"#")+"'";
       DBConn.GetRS();
       if (DBConn.RSIsEmpty()==true)
       {
        DBConn.DBClose();
        DBConn=null;
        if (this.ErrorURL!="")
         Response.Redirect(this.ErrorURL);
       }
       else
       {
        if (this.SessionOfName!="")
        {
         Session(this.SessionOfName)=String(DBConn.RS(this.UnameField));
        }
        if (this.SessionOfPwd!="")
        {
         Session(this.SessionOfPwd)=String(DBConn.RS(this.PwdField));
        }
        if (this.SessionOfLevel!="")
        {
         Session(this.SessionOfLevel)=String(DBConn.RS(this.LevelField));
        }
        DBConn.CloseRS();
        DBConn.DBClose();
        DBConn=null;
        if (this.OKURL!="")
         Response.Redirect(this.OKURL);
       }
      }
     }
    %>

      测试页面代码如下:
    <%@LANGUAGE="JAVASCRIPT" CODEPAGE="936"%>
    <!--#include FILE="DBO.asp"-->
    <%
    var DBobj=new DBOperate();
    DBobj.CurrentPageTag="PID";
    DBobj.DBType=2;
    DBobj.GetDBPath();
    DBobj.DBOpen();
    DBobj.SQLString="select * from members";
    DBobj.PageSize=4;
    DBobj.GetRS();
    //********************************************************************
    if (!DBobj.RSIsEmpty())
    {
    DBobj.RS.MoveFirst();
    DBobj.RS.Move((DBobj.CurrentPageID-1)*DBobj.PageSize,1);
    var j=0;
    while (!DBobj.RS.eof && j<DBobj.PageSize)//
    {
     Response.Write(DBobj.RS("name")+"<br>");
     j=j+1;
     DBobj.RS.MoveNext();
    }
    }
    //*********************************************************************
    Response.Write("<br>");
    //*********************************************************************
    Response.Write(DBobj.SetPager());   //分页导航条高度为25
    //*********************************************************************
    Response.Write(CutString("啊撒123娇散件 aaaa","#",8));
    var Uobj=new UserLogin();
    Uobj.UnameField="uname";
    Uobj.PwdField="upwd";
    Uobj.LevelField="ulevel";
    Uobj.UserTableName="uadmi";
    Uobj.uname="sadmin";
    Uobj.pwd="sadmin";
    Uobj.OKURL="";
    Uobj.ErrorURL="2.htm";
    Uobj.SessionOfName="aaa";
    DBobj.CloseRS();
    DBobj.DBClose();
    DBobj=null;
    Uobj.CheckLogin();
    Response.Write(Session(Uobj.SessionOfName));
    %>
      具体效果把上面的代码存成2个asp文件测试就知道了,不多说了。
  • 相关阅读:
    Python学习笔记21:数据库操作(sqlite3)
    JAVA的extends使用方法
    thinkphp5的Illegal string offset 'id'错误
    thinkphp5项目--个人博客(五)
    语法错误: unexpected ''); ?></span></span></h2> ' (T_CONSTANT_ENCAPSED_STRING), expecting ',' or ';'
    thinkphp5.0的验证码安装和相关错误
    thinkphp5项目--个人博客(四)
    thinkphp5项目--个人博客(三)
    NAS是什么
    百度编辑器简介及如何使用
  • 原文地址:https://www.cnblogs.com/zxub/p/173593.html
Copyright © 2011-2022 走看看