JavaScript connecting to an Access database complete instance _javascript tips

Source: Internet
Author: User
Tags tagname access database getv

The example in this article describes how JavaScript connects to an Access database. Share to everyone for your reference. The implementation method is as follows:

var roc = Roc | |
{}; roc.db = Roc.db | |
{}; Create a connection roc.db.createDb = function () {var conn = new ActiveXObject ("ADODB. Connection "), FSO = new ActiveXObject (" Scripting.FileSystemObject "), connstr =" provider=microsoft.jet.oledb.4.0; " Data source= "+ FSO.
  GetFile ("./db/sigma.mdb"); Conn.
  Open (CONNSTR);//Opening database roc.db.conn = conn;
return roc.db.conn;
};
  Gets the connection roc.db.getDb = function () {if (roc.db.conn) {return roc.db.conn;
  }else{return roc.db.createDb ();
}
};
    Close Connection Roc.db.closeConn = function () {if (roc.db.conn) {roc.db.conn.close ();
  Roc.db.conn = null;
}
};
  Gets the result set roc.db.getRs = function (sqlstr) {var mysql = Roc.dom.trim (sqlstr);
  if (mysql = = ") {return;} var rs = new ActiveXObject ("ADODB.
  Recordset "), myconn = Roc.db.getDb (); 
  Rs.Open (Sqlstr, myconn); 
Return RS;
};
  Closes the result set Roc.db.closeRs = function (rs) {rs.close ();
RS =null;
};
  UPDATE, insert Roc.db.execute = function (sqlstr) {var myconn = roc.db.getDb (); Myconn.execUte (SQLSTR);
Roc.db.closeConn ();
}; *---------Sigma: "I let you trample on my dignity and not be angry, because I love you." "---------* * roc.dom = Roc.dom | |
{}; 
  Roc.dom.id = function (ID) {if (typeof id = = ' string ' | | | id instanceof string) {return document.getElementById (ID);
  else if (id && id.nodename && (id.nodetype = = 1 | | | id.nodetype = = 9)) {return id;  
return null;
}; /** * @method tagName to get the specified DOM element * @param {String} tagName element Label name * @param {htmlelement} el element belongs to the Document object defaults to the current document * @re
  Turn {HtmlElement} returns the HtmlElement element */roc.dom.tagName = function (TagName, EL) {var el = el | | document;
Return El.getelementsbytagname (TagName);
}; Delete the left and right sides of the space Roc.dom.trim = function (str) {return (str+ '). Replace (/(^\s*) | (
\s*$)/g, ""); /** * @method Show Target element * @param {element} element target element or target element ID * @param {String} element target elements/roc.dom.show = fun
  Ction (Element) {element = Roc.dom.id (element);
  Element.style.display = ';
return element;
}; /** * @method Hide hidden Target element * @param {element} element the ID of the target element or target element * @param {String} element target elements */roc.dom.hide = function (Element) {element =
  Roc.dom.id (Element);
  Element.style.display = ' None ';
return element;
}; /** * @method Hasclass determine if the element contains class * @param {element} el Element * @param {String} className class name * * Roc.dom.hasClas
  s = function (el, className) {var re = new RegExp (' (^|\\s) ' + ClassName + ' (\\s|$) ');
Return Re.test (El.classname);
}; /** * @method addclass to add class * @param {element} el Element * @param {String} className class name * * Roc.dom.addClass
  = function (el, className) {if (!roc.dom.hasclass (el, ClassName)) {el.classname = el.classname + ' + className;
}
}; /** * @method Removeclass to remove class * @param {element} el Element * @param {String} className class name * * Roc.dom.removeC Lass = function (el, className) {var re = new RegExp (' (^|\\s) ' + ClassName + ' (?: \
\s|$) ' El.classname = El.className.replace (Re, ' $ ');
}; /** * Date Object Namespace * * @namespace * @name data
* * roc.date = Roc.date | |
{};  /** * @method Format the target Date object is formatted * @param {object} timestamp target Date Object * @return {String} str formatted time * * Roc.date.format
  = function (timestamp) {if (timestamp = = ") return";
  var str = ', Temptime = new Date (number (timestamp));
  STR + + temptime.getfullyear () + '-';
  STR + + temptime.getmonth () + 1 + '-';
  STR + + temptime.getdate () + '; str + = String (Temptime.gethours ()). length > 1?
  (temptime.gethours () + ': '): (' 0 ' + temptime.gethours () + ': '); str + = String (Temptime.getminutes ()). length > 1?
  (Temptime.getminutes ()): (' 0 ' + temptime.getminutes ());
return str;
}; /** * Cookie Object Namespace * * @namespace * @name cookie/roc.cookie = Roc.cookie | |
{};
 /** * @method Set * @param {string} name cookie Key * @param {string} value Cookie * @param {string} Expires expiration time (hours) * @param {string} domain Domains * @param {string} path Path * @param {string} Secure support HTTPS/Roc.cookie.set = f Unction (name, value, expires, domain, path, secure) {var text = encodeURIComponent (value), date = expires;
    if (date && typeof date = = ' number ') {date = new date ();
  Date.settime (Date.gettime () + (expires * 3600000));
  } if (date instanceof date) {text + = '; expires= ' + date.toutcstring ();
  } if (domain) {text = = '; domain= ' + domain;
  } if (path) {text + = '; path=/' + path;
  else {text + = '; path=/';
  } if (secure) {text + = '; secure ';
} document.cookie = name + ' = ' + text;
};
  /** * @method Get * @param {String} name Cookie's key */roc.cookie.get = function (name) {var ret, m; if (name) {if (M = Document.cookie.match (' (?: ^|) ' + name + ' (?:(?: = ([^;] *))|;|
    $))) {ret = m[1]? decodeuricomponent (m[1]): ';
} return ret;
}; Roc.util = Roc.util | |
{};
      Roc.util.loger = function (type, msg) {switch (type) {case ' Pop ': Alert (msg);
    Break
    Case ' float ': break;
  Default:break;
}
}; Roc.util.resultBlink = function ( msg) {//Operation flashing hint var $ = roc, opt = $.dom.id ("Opttip"); $.util.toogle = $.util.toogle | |
  0;
  Cleartimeout (ROC.UTIL.T)//debug opt.innerhtml = msg;
  $.dom.show (opt);
  Opt.classname = "blink" + $.util.toogle%2;
  $.util.toogle++;
  roc.util.t = settimeout (function () {$.dom.hide (opt); },$.config.
Blink_delay);
};
  Roc.util.onlyInputNumber = function (ID) {//Limit text box, text field can only enter the number var $ = roc, num = $.dom.id (ID);
  if (num.tagName.toLowerCase ()!= ' input ' | | | num.tagName.toLowerCase ()!= ' textarea ') {return;
    } $.util.addevent (num, ' keypress ', function (e) {var e = e | | window.event;
    if (E.keycode >= && e.keycode <=) {alert () return true;
  return false;
});
}; Roc.util.addEvent = function (Elem, type, FN, usecapture) {if (Elem.addeventlistener) {//dom2.0 Elem.addeventlistene
    R (Type, FN, usecapture);
  return true;
    else if (elem.attachevent) {//ie5+ elem.attachevent (' on ' + type, fn); Return TruE
  else {//dom 0 elem[' on ' + type] = fn;
}
}; Roc.config = Roc.config | |
{}; Roc.config = Roc.config | | {blink_delay:3000, select_delay:1000} roc.search = Roc.search | |
 {}; Roc.search.getValues = function (e) {//Bulk get form value, used to insert var $ = roc, Allisnull = true, Wrapstr = function (
    num) {return ' "' + num + '";
  }, Vals =[];
    for (Var i in e[0]) {var v =$.dom.trim ($.dom.id (E[0][i)). Value + ');
    if (v!= ') {allisnull = false;
        Switch (E[1][i]) {case ' date ': Case ' text ': Vals.push (Wrapstr (v));
      Break
        Case ' num ': Vals.push (v);
      Break
    Default:break;
  } if (Allisnull) {return false;
Return Vals.join (', ');
}; ROC.SEARCH.GETSELSQL = function () {//Assembly search SQL var $ = roc, addr = $.dom.trim ($.dom.id ("S_uaddr"). Value), phone = $.dom.trim ($.dom.id ("S_uphone"). Value), style = $.dom.trim ($.dom.id ("S_style"). Value), year = $.dom.trim ($.doM.id ("S_year"). Value), month = $.dom.trim ($.dom.id ("S_month"). Value), date = $.dom.trim ($.dom.id ("S_date"). Value) , Datetype = $.dom.trim ($.dom.id ("S_datetype"). Value), MySQL = ' select * from inslist where 1=1 ', Datetypenam E = Datetype = 0?
  ' Selltime ': ' Addtime ';  
  if (addr!= ') {mysql + = ' and uaddr like '% ' + addr + '% ';
  } if (phone!= ') {mysql = ' and Uphone = ' + Phone + ' ";
  } if (style!= ') {mysql + = ' and typeID = ' + style + ';
  } if (year!= ') {mysql + = ' + Datetypename + '] = ' + year + ';
  } if (month!= ') {mysql + = ' and month (' + datetypename + ') = ' + month + ';
  } if (date!= ') {mysql + = ' and date (' + Datetypename + ') = ' + Date + ';
return to MySQL;
};
  Search roc.search.seeking = function () {if (!roc.search.getlock ()) {return;}
    var $ = roc, MYSQL = $.search.getselsql ();
  html = $.search.getsel (MYSQL);
  $.search.setlock (FALSE); $.dom.id ("SearchResult"). InnerHTML= html;
$.util.resultblink ("Query Complete");/flashing}; Roc.search.getSel = function (SQLSTR) {//query var $ = roc, rs = $.db.getrs (sqlstr), filtrs = function (str) {//processing field return (str + ') = ' null '?
  ': STR;
  }, num = 1;
      total_receive = 0, total_prize = 0, html = "<table class= ' list ' id= ' memorydetails ' >" + "<colgroup>" + "<col class= ' pid '/>" + "<col class= ' uaddr '/>" + "<col class= ' phone '/>" + "& Lt;col class= ' number '/> "+" <col class= ' money ' span= ' 3 '/> "+" <col class= ' number '/> "+" <col class= ' phone '/> "+" <col class= ' number '/> "+" <col class= ' Date '/> "+" <col class= ' Date '/> "+" </colgroup> "+" <tr class= ' Secondrow donotfilter ' > "+" <th class= ' PID ' > Serial </th> ' + ' <th class= ' uaddr ' > User address </th> ' + ' <th class= ' phone ' > Subscriber ;/th> "+" <th class= ' number ' > Model </th> "+" <th class= ' Money ' > Collection </th> "+" <th class= ' money ' > Payment
      </th> "+" <th class= ' money ' > Balance </th> "+" <th class= ' number ' > Installer </th> " + "<th class= ' phone ' > Sales Tel </th>" + "<th class= ' number ' > Memo </th>" + "<th clas
  s= ' Date ' > Sales date </th> ' + ' <th class= ' date ' > record time </th> ' + ' </tr> '; while (!rs. EOF) {var id = num,//filtrs (Rs. Fields ("id")), Uaddr = Filtrs (rs. Fields ("Uaddr")), Uphone = Filtrs (rs. Fields ("Uphone")), typeID = Filtrs (rs. Fields ("typeID")), Received = Filtrs (Rs. Fields ("received")), prize = Filtrs (Rs. Fields ("Prize")), unreceived = Filtrs (rs. Fields ("unreceived")), Installerid = Filtrs (rs. Fields ("Installerid")), Sellerid = Filtrs (rs. Fields ("Sellerid")), Remark = Filtrs (Rs. Fields ("remark")), Selltime = $.date.format (filtrS (Rs. Fields ("Selltime")), Addtime = $.date.format (Filtrs (Rs.
    Fields ("Addtime")); HTML + + "<tr jsselect= ' Browzr_data ' >" + "<td class= ' pid ' >" + ID + "</td>" + "<td class= ' uaddr ' & gt; "+ uaddr +" </td> "+" <td class= ' phone ' > "+ uphone +" </td> "+" &LT;TD class= ' number ' > "+ t  Ypeid + "</td>" + "<td class= ' money ' >" + Received + "</td>" + "<td class= ' money ' >" + prize +  "</td>" + "<td class= ' money ' >" + unreceived + "</td>" + "&LT;TD class= ' number ' >" + Installerid + "</td>" + "<td class= ' phone ' >" + Sellerid + "</td>" + "&LT;TD class= ' number ' >" + Remark + "& Lt;/td> "+" <td class= ' Date ' > "+ selltime +" </td> "+" <td class= ' Date ' > "+ addtime +" </td&
    gt; "
    + "</tr>";
    Statistics items Total_receive + = received, total_prize + = prize, num++;
  Rs.movenext (); HTML = html + "<tr class= ' total DonotfiLter ' > "+" <td class= ' pid ' ></td> "+" <td class= ' uaddr ' >Σ</td> "+" &LT;TD class= ' number ' > </td> "+" &LT;TD class= ' number ' ></td> "+" &LT;TD class= ' number ' > "+ total_receive +" </td> "+" & LT;TD class= ' number ' > "+ total_prize + </td>" + "&LT;TD class= ' number ' >" + (total_prize-total_receive) + "& Lt;/td> "+" &LT;TD class= ' number ' ></td> "+" &LT;TD class= ' number ' ></td> "+" &LT;TD class= ' number ' &
  Gt;</td> "+" <td class= ' date ' ></td> ' + ' <td class= ' date ' ></td> ' + ' </table> ';
  $.db.closers (RS); 
  $.db.closeconn ();
return HTML;
}; Roc.search.getLock = function () {//Query lock if (typeof Roc.search.searchLock = = ' undefined ') {Roc.search.setLock (false
  );
return roc.search.searchLock;
};
Roc.search.setLock = function (key) {roc.search.searchLock = key;};
        [[Id],[type]] Roc.search.addEls = [["Uaddr", "Uphone", "typeID","Received", "prize", "Unreceived", "Installerid", "Sellerid", "remark", "s Elltime "],[' text", ' text ', ' num ', ' num ', ' num ', ' num ', '
Num ', ' num ', ' text ', ' Date ']];
  Roc.search.insert = function () {//Insert installation single record var $ = roc, Getv = $.search.getvalues ($.search.addels); if (!getv) {$.util.loger (' pop ', ' please fill in the information before saving!)
    '); 
  Return var sqlstr = ' INSERT into inslist (Uaddr,uphone,typeid,received,prize,unreceived,installerid,sellerid,remark,
  Selltime) VALUES (' + Getv + ') ';
  $.db.execute (SQLSTR);
$.util.resultblink (' Save installation single success ');
};
  /* Show and hide/* Roc.dom.switchDiv = function (Objdiv) {var $ = roc, CookieName = objdiv.id + ' cookie ';
    if (ObjDiv.style.display = = ' | | objDiv.style.display = = ' None ') {$.dom.show (objdiv);
  $.cookie.set (cookiename,0,9999999);
    }else{$.dom.hide (OBJDIV); $.cookie.set (cookiename,1,9999999);
}
}; Cargo model Operation Roc.tstyle = Roc.tstyle | |
{};
Roc.tstyle.els = [[' Tname ', ' tprize ', ' Tdesc '], [' Text ', ' text ', ' text ']];
  Roc.tstyle.insert = function () {//insert record var $ = roc, Getv = $.search.getvalues ($.tstyle.els); if (!getv) {$.util.loger (' pop ', ' please fill in the information before saving!)
    '); 
  Return
  var sqlstr = ' INSERT into type (Tname, tprize, Tdesc) VALUES (' + Getv + ') ';
  $.util.loger (' Pop ', sqlstr);
  $.db.execute (SQLSTR); $.util.resultblink (' Save success!
  ');
$.util.flushinput ($.tstyle.els);
};
  Roc.util.flushInput = function (els) {var $ = ROC;
    for (var i = 0; i < els.length i + +) {var e = $.dom.id (els[i]+ ');
    /* if (e.tagname = = ' input ' && e.type = = ' text ') {* * * * e.value = ';
/*}*/
  }
}; Type{id,tname,tprize} Roc.tstyle.getStyle = function (Optid) {//Get type list var $ = roc, MYSQL = ' select * from Typ e where Isdel = 0 ', rs = $.db.getrs (MYSQL), filtrs = function (str) {//Processing field return (str+ ') = = 'Null '?
  ': STR;
  }, myopt = $.dom.id (optid), optindex = 1; while (! Rs. EOF) {var id = filtrs (rs. Fields (' id ')), prize = Filtrs (Rs. Fields (' tprize ')), name = Filtrs (Rs.
      Fields (' tname ')); desc = filtrs (rs.
    Fields (' Tdesc '));
    Myopt.options[optindex] = new Option (name, id); Myopt.options[optindex].title = ' Price: ' + prize + ' |
    Description: ' + desc;
    optindex++;
  Rs.movenext ();
  } $.db.closers (RS); 
$.db.closeconn ();
};
  ;(function () {var $ = ROC;
  $.dom.id ("Save"). onclick = function () {//save $.search.insert ();
  } $.dom.id ("Searchbtn"). onclick = function () {//check $.search.seeking (); ///Initialize query installation single year for (var i = 0; I <= i++) {$.dom.id ("S_year"). Options[i] = new Option (+ I, + I
    );
    if (2010+i+ ' = = (new Date ()). getyear ()) {$.dom.id ("S_year"). Options[i].selected = true;
  }//Initialize query installation single month for (var i = 1; I <= i++) {$.dom.id ("S_month"). Options[i] = new Option (i,i); }//Extraction condition FieldID Modify trigger Query $.dom.s_fields = ["S_uaddr", "S_uphone", "S_style", "S_datetype", ' s_year ', ' s_month ', ' s_date '];
    for (var i = 0; i < $.dom.s_fields.length i + +) {var f = $.dom.s_fields[i];
        $.dom.id (f). Onpropertychange = function () {if (Event.propertyname = = ' value ') {$.search.setlock (true);
        if ($.search.t) {cleartimeout ($.search.t);
        } $.search.t = settimeout (function () {$.search.seeking (); },$.config.
      Select_delay);
    } $.dom.id (f). onfocus = function () {$.dom.addclass (this, "focusit");
    };
    $.dom.id (f). onblur = function () {$.dom.removeclass (this, "focusit");
  };
  } $.dom.id (' Savetype '). onclick = function () {//cargo type $.tstyle.insert ();
  };
  Remove Type list $.tstyle.getstyle (' typeid ');
$.tstyle.getstyle (' S_style '); /*//$.dom.id ("s_uaddr"). onkeyup = $.dom.id ("S_uphone"). onkeyup = $.dom.id ("S_style"). onkeyup = function () {$.dom.id (" S_uaddr "). onblur = $.dom.id (" S_uphone "). onblur= $.dom.id ("S_style"). onblur = function () {$.dom.removeclass (this, "focusit"); $.dom.id ("S_uaddr"). onfocus = $.dom.id ("S_uphone"). onfocus = $.dom.id ("S_style"). onfocus = function () {$.DOM.ADDCL
  Ass (This, "focusit"); }*///Navigation style toggle for (var i = 0; i < $.search.addels.length i++) {var curobj = $.dom.id ($.search.addels[0][i]+ "
    );
    Curobj.onfocus = function () {$.dom.addclass (this, ' focusit ');
    } Curobj.onblur = function () {$.dom.removeclass (this, ' focusit ');
  }//Add navigation click event var lis = $.dom.tagname (' li ', $.dom.id ("Ulnav"));
    for (var i = 0; i < lis.length i + +) {$.dom.hide ($.dom.id (lis[i].id + ' Div '));
      Lis[i].onclick = function () {for (var n = 0; n < lis.length; n + +) {$.dom.removeclass (lis[n], ' click ');
    $.dom.hide ($.dom.id (lis[n].id + ' Div '));
    } $.dom.show ($.dom.id (this.id + "Div"));
    $.dom.addclass (This, "click");
  $.cookie.set (' Showwhichdiv ', this.id); }///default load Display page var ShowwHichdiv = $.cookie.get ("Showwhichdiv") | |
  "SearchList";
  $.dom.addclass ($.dom.id (Showwhichdiv), "click");
  $.dom.show ($.dom.id (showwhichdiv + ' Div '));
  Date control, thanks to this control developer's share, wish you have a good girlfriend!
  J (' #selltime '). Calendar ({format: ' Yyyy-mm-dd HH:mm:ss '});
  var numfields = [' S_uphone ', ' s_date ', ' uphone ', ' received ', ' Prize ', ' unreceived ', ' Installerid '];
  for (var i = 0; i < numfields.length i + +) {$.util.onlyinputnumber (numfields[i]);

 }
})();

The

wants this article to help you with your JavaScript programming.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.