Javascript connection to Access database full instance _ javascript skills

Source: Internet
Author: User
Tags getv
This article mainly introduces how to connect Javascript to the Access database. It involves common operations such as connection, closure, addition, deletion, modification, and query of the access Database by javascript, which has some reference value, for more information about how to connect Javascript to the Access database, see the example in this article. Share it with you for your reference. The specific 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); // Open the roc of the database. db. conn = conn; return roc. db. conn ;}; // obtain the roc of the connection. db. getDb = function () {if (roc. db. conn) {return Roc. db. conn;} else {return roc. db. createDb () ;}}; // closes the roc connection. db. closeConn = function () {if (roc. db. conn) {roc. db. conn. close (); roc. db. conn = null ;}}; // obtain the roc of the result set. 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 ;}; // close the roc of the result set. db. closeRs = function (Rs) {rs. close (); rs = null;}; // The updated version is inserted into roc.db.exe cute = function (sqlStr) {var myConn = roc. db. getDb (); myConn.exe cute (sqlStr); roc. db. closeConn () ;};/* --------- Sigma: "I'm not angry when you trample on my dignity 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 obtains the specified dom element based on the tag * @ param {String} tagName element tag name * @ param {HTMLElement} The document object to which the el Element belongs is the current document by default * @ return {HTMLElement} returns HTMLElemen Telement */roc. dom. tagName = function (tagName, el) {var el = el | document; return el. getElementsByTagName (tagName) ;}; // Delete the spaces between the left and right sides of roc. dom. trim = function (str) {return (str + ''). replace (/(^ \ s *) | (\ s * $)/g ,"");} /*** @ method show: display the target Element * @ param {element} the target element or the id of the target Element * @ param {String} element target element */roc. dom. show = function (element) {element = roc. dom. id (element); element. style. display = ''; Return element ;}; /*** @ method hide hides the target Element * @ param {element} element or the id of the target Element * @ param {String} element target element */roc. dom. hide = function (element) {element = roc. dom. id (element); element. style. display = 'none'; return element ;}; /*** @ method hasClass determines whether the Element contains the class * @ param {Element} el Element * @ param {String} className class name */roc. dom. hasClass = function (el, className) {var re = new RegExp ('( ^ | \ S) '+ className +' (\ s | $) '); return re. test (el. className) ;};/*** @ method addClass add class * @ param {Element} el Element * @ param {String} className class name */roc to the Element. dom. addClass = function (el, className) {if (! Roc. dom. hasClass (el, className) {el. className = el. className + ''+ className ;}}; /*** @ method removeClass removes class from the Element * @ param {Element} el Element * @ param {String} className class name */roc. dom. removeClass = function (el, className) {var re = new RegExp ('(^ | \ s)' + className + '(?: \ S | $) ') el. className = el. className. replace (re, '$ 1') ;};/*** date object namespace ** @ namespace * @ name data */roc. date = roc. date | {}; /*** @ method format the target date Object * @ param {Object} timestamp the 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 + = temp Time. 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 value * @ param {String} expires expiration time (hours) * @ param {String} domain * @ param {String} path * @ param {String} whether secure supports https */roc. cookie. set = function (name, val Ue, 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 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 blinking prompt var $ = roc, opt = $. dom. id ("optTip"); $. util. toogle = $. util. toogle | 0; clearTimeout (roc. util. t); // debug opt. innerHTML = msg; $. dom. show (opt); opt. class Name = "blink" + $. util. toogle % 2; $. util. toogle ++; roc. util. t = setTimeout (function () {$. dom. hide (opt) ;}, $. config. BLINK_DELAY) ;}; roc. util. onlyInputNumber = function (id) {// you can only enter numbers in the restricted text box or text field var $ = roc, num = $. dom. id (id); if (num. tagName. toLowerCase ()! = 'Input' | num. tagName. toLowerCase ()! = 'Textea ') {return;} $. util. addEvent (num, 'keypress ', function (e) {var e = e | window. event; if (e. keyCode> = 48 & e. keyCode <= 57) {alert () return true ;}return false ;}) ;}; roc. util. addEvent = function (elem, type, fn, useCapture) {if (elem. addEventListener) {// DOM2.0 elem. addEventListener (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) {// obtain the form values in batches, 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]). v Alue + ''); 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 () {// assemble and 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_styl E "). 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', datetypeName = datetype = 0? 'Shelltime': 'addtime'; if (addr! = '') {Mysql + = 'and uaddr like" %' + addr + '% "';} if (phone! = '') {Mysql + = 'and uphone ="' + phone + '"';} if (style! = '') {Mysql + = 'and typeid =' + style +'';} if (year! = '') {Mysql + = 'and year (' + datetypeName + ') =' + year +'';} if (month! = '') {Mysql + = 'and month (' + datetypeName + ') =' + month +'';} if (date! = '') {Mysql + = 'and date (' + datetypeName + ') =' + date +'';} return mysql ;}; // search for 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 completed"); // blinking}; roc. search. getSel = function (sqlStr) {// query var $ = roc, rs = $. db. getRs (sqlStr), filtRs = function (str) {// process the field return (str + '') = 'null '? '': Str ;}, num = 1; total_receive = 0, total_prize = 0, html ="
 
 
  
  
"+"
  
  "+"
  
  "+"
  
  "+"
  
  "+"
  
  "+"
  
  "+"
  
  "+"
  
  "+"
  
  "+"
  
  "+"
  
  "+""+"
  
  
     "+" 
    "+" 
    "+" 
    "+" 
    "+" 
    "+" 
    "+" 
    "+" 
    "+" 
    "+" 
    "+" 
    "+" 
    "+" 
   "; 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"), encoded ED = filtRs (rs. fields ("received"), prize = filtRs (rs. fields ("prize"), uninitialized ED = filtRs (rs. fields ("uninitialized ed"), 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 + =" 
   
     "+" 
    "+" 
    "+" 
    "+" 
    "+" 
    "+" 
    "+" 
    "+" 
    "+" 
    "+" 
    "+" 
    "+" 
    "+" 
   "; // Statistical item total_receive + = received, total_prize + = prize, num ++; rs. moveNext ();} html = html +" 
   
     "+" 
    "+" 
    "+" 
    "+" 
    "+" 
    "+" 
    "+" 
    "+" 
    "+" 
    "+" 
    "+" 
    "+" 
    "+" 
   
Serial numberUser addressUser phone numberModelCollection on behalfPaymentBalanceInstallerSales phone numberRemarksSales dateRecord time
"+ Id +""+ Uaddr +""+ Uphone +""+ Typeid +""+ Received +""+ Prize +""+ Uninitialized ed +""+ Installerid +""+ Sellerid +""+ Remark +""+ Selltime +""+ Addtime +"
Σ"+ Total_receive +""+ Total_prize +""+ (Total_prize-total_receive) +"
"; $. Db. closeRs (rs); $. db. closeConn (); return html;}; roc. search. getLock = function () {// query the 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", "uninitialized ed", "installerid", "sellerid "," Remark "," selltime "], ['text', 'text', 'num ', 'num', 'text', 'date']; roc. search. insert = function () {// insert the installation ticket record var $ = roc, getV = $. search. getValues ($. search. addEls); if (! GetV) {$. util. loger ('pop', 'fill in the information and save it! '); Return;} var sqlStr = 'insert into inslist (uaddr, uphone, typeid, received, prize, uninitialized ed, installerid, sellerid, remark, selltime) values ('+ getV +') '; 2.16.db.exe cute (sqlStr); $. util. resultBlink ('installation ticket saved successfully');};/* display and hide */roc. dom. switchDiv = function (objDiv) {var $ = roc, cookieName = objDiv. id + 'cookies'; if (objDiv. style. display = ''| objDiv. style. display = 'None') {$. dom. show (objDiv); $. coo Kie. set (cookieName,);} else {$. dom. hide (objDiv); $. cookie. set (cookieName, 1,9999999) ;}; // roc for cargo model operation. 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', 'fill in the information and save it! '); Return;} var sqlStr = 'insert into type (tname, tprize, tdesc) values (' + getV + ')'; // $. util. loger ('pop', sqlStr); pai.db.exe cute (sqlStr); $. util. resultBlink ('saved successfully! '); $. 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) {// obtain the type list var $ = roc, mySql = 'select * from type where isdel = 0', rs = $. db. getRs (mySql), filtRs = Function (str) {// process the 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 and query the installation period for (var I = 0; I <= 10; I ++) {$. dom. id ("s_year "). options [I] = new Option (2010 + I, 2010 + I); if (2010 + I + ''= (new Date ()). getYear () {$. dom. id ("s_year "). options [I]. selected = true ;}/// initialization query installation month for (var I = 1; I <= 12; I ++) {$. dom. id ("s_month "). options [I] = new Option (I, I );} // Modify the id of the detection condition field to trigger the 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 () ;}, $. conf Ig. 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 () {// goods type $. tstyle. insert () ;}; // retrieve the type list $. tstyle. getStyle ('typeid'); $. tstyle. getStyle ('s _ style ');/* // $. dom. id ("s_uaddr "). onkeyup = $. dom. id ("s_uphone "). onkeyup = $. dom. id ("s_style "). onkeyup = f Unction () {$. 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. addClass (this, "focusit");} * // switch the navigation style for (var I = 0; I <$. search. addEls. length; I ++) {var curObj = $. dom. id ($. search. addEls [0] [I] + ''); cur Obj. onfocus = function () {$. dom. addClass (this, 'focusit ');} curObj. onblur = function () {$. dom. removeClass (this, 'focusit ') ;}// Add a navigation Click Event var lis =$. dom. tagName ('lil', $. 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) ;}} // The default loading display page var showWhichDiv =$. cookie. get ("showWhichDiv") | "searchList"; $. dom. addClass ($. dom. id (showWhichDiv), "click"); $. dom. show ($. dom. id (showWhichDiv + 'div '); // Date control. Thank you for sharing this control and wish you 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]) ;}} ();

I hope this article will help you design javascript programs.

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.