This article describes the method of connecting to Access database by Javascript. Share it for your reference. The specific implementation method is as follows:
var roc = roc || {}; = ||{}; //Create a connection = function(){ var conn = new ActiveXObject(""), fso = new ActiveXObject(""), connstr = "Provider=.4.0;Data Source=" + ("./db/"); (connstr);//Open the database = conn; return ; }; //Get the connection = function(){ if( ){ return ; }else{ return (); } }; //Close the connection = function(){ if( ){ (); = null; } }; //Get the result set = function( sqlStr ){ var mysql = ( sqlStr ); if(mysql == ''){return;} var rs = new ActiveXObject(""), myConn = (); ( sqlStr , myConn ); return rs; }; //Close the result set = function( rs ){ (); rs =null; }; //Update, insert = function( sqlStr ){ var myConn = (); ( sqlStr ); (); }; /*------------Sigma: "I let you trample on my dignity without getting angry at all because I love you." ----------*/ = ||{}; = function( id ){ if(typeof id == 'string' || id instanceof String) { return (id); } else if(id && && ( == 1 || == 9)) { return id; } return null; }; /** * @method tagName Get the specified dom element according to the tag * @param {String} tagName element tag name * @param {HTMLElement} The document object to which the el element belongs is the default to the current document * @return {HTMLElement} Return HTMLElement element */ = function(tagName, el) { var el = el || document; return (tagName); }; //Delete the spaces on both left and right ends = function (str) { return (str+'').replace(/(^\s*)|(\s*$)/g, ""); } /** * @method show Display target element * @param {Element} element The target element or the id of the target element * @param {String} element Target element */ = function (element) { element = (element); = ''; return element; }; /** * @method hide Hide target element * @param {Element} element The target element or the id of the target element * @param {String} element Target element */ = function (element) { element = (element); = 'none'; return element; }; /** * @method hasClass determines whether the element contains class * @param {Element} el element * @param {String} className class name */ = function(el, className){ var re = new RegExp('(^|\\s)' + className + '(\\s|$)'); return (); }; /** * @method addClass Add class to the element * @param {Element} el element * @param {String} className class name */ = function(el, className){ if(!(el, className)){ = + ' ' + className; } }; /** * @method removeClass Remove class to element * @param {Element} el element * @param {String} className class name */ = function(el, className){ var re = new RegExp('(^|\\s)' + className + '(?:\\s|$)') = (re, '$1'); }; /** * date object namespace * * @namespace * @name data */ = || {}; /** * @method format formats the target date object * @param {Object} timestamp Target date object * @return {String} str Formatted time */ = function(timestamp) { if(timestamp =='' )return ''; var str = '', temptime = new Date(Number(timestamp)); str += () + '-'; str += () + 1 + '-'; str += () + ' '; str += String(()).length > 1 ? (() + ':') : ('0' + () + ':'); str += String(()).length > 1 ? (()) : ('0' + ()); return str; }; /** * Cookie object namespace * * @namespace * @name cookie */ = || {}; /** * @method set * @param {String} key of name cookie * @param {String} value cookie value * @param {String} expires Expires Time of expiration (hours) * @param {String} domain domain domain * @param {String} path * @param {String} secure supports https */ = function(name, value, expires, domain, path, secure) { var text = encodeURIComponent(value), date = expires; if(date && typeof date === 'number') { date = new Date(); (() + (expires * 3600000)); } if(date instanceof Date) { text += '; expires=' + (); } if(domain) { text += '; domain=' + domain; } if(path) { text += '; path=/' + path; } else { text += '; path=/'; } if(secure) { text += '; secure'; } = name + '=' + text; }; /** * @method get * @param {String} key of name cookie */ = function(name) { var ret, m; if(name) { if((m = ('(?:^| )' + name + '(?:(?:=([^;]*))|;|$)'))) { ret = m[1] ? decodeURIComponent(m[1]) : ''; } } return ret; }; = || {}; = function( type ,msg ){ switch(type){ case 'pop': alert(msg); break; case 'float':break; default:break; } }; = function( msg ){ //Operation flashing prompt var $ = roc , opt = $.("optTip"); $. = $. || 0; clearTimeout();//debug = msg ; $.(opt); = "blink" + $.%2; $.++; = setTimeout(function(){ $.(opt); },$.config.BLINK_DELAY); }; = function( id ){ //Restrict text boxes and text fields to enter numbers only var $ = roc , num = $.( id ); if( () != 'input' || () != 'textarea' ){ return ; } $.( num , 'keypress' , function( e ){ var e = e || ; if( >= 48 && <= 57){alert() return true; } return false; }); }; = function(elem, type, fn, useCapture) { if() { //DOM2.0 (type, fn, useCapture); return true; } else if() { //IE5+ ('on' + type, fn); return true; } else { //DOM 0 elem['on' + type] = fn; } }; = || {}; = ||{ BLINK_DELAY:3000, SELECT_DELAY:1000 } = || {}; = function( e ){ //Batch form values for insertion var $ = roc, allIsNull = true, wrapStr = function( num ){ return '"'+ num + '"'; }, vals =[]; for(var i in e[0]){ var v =$.($.( e[0][i] ).value + ''); if( v != ''){ allIsNull = false; } switch( e[1][i] ){ case 'date': case 'text': ( wrapStr(v) ); break; case 'num': ( v ); break; default:break; } } if(allIsNull){ return false; } return (','); }; = function(){ //Assemble and search for sql var $ = roc, addr = $.($.("s_uaddr").value), phone = $.($.("s_uphone").value), style = $.($.("s_style").value), year = $.($.("s_year").value ), month = $.($.("s_month").value), date = $.($.("s_date").value), datetype = $.($.("s_datetype").value), mysql = 'select * from inslist where 1=1 ', datetypeName = 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 += ' and year(' + datetypeName + ') = ' + year + ''; } if( month !=''){ mysql += ' and month(' + datetypeName + ') = ' + month + ''; } if( date !=''){ mysql += ' and date(' + datetypeName + ') = ' + date + ''; } return mysql; }; //search = function(){ if( !()){return;} var $ = roc , mySql = $.(); html = $.( mySql); $.(false); $.("searchResult").innerHTML = html; $.("Query Completed");//Blinking}; = function( sqlStr ){ //Inquiry var $ = roc, rs = $.( sqlStr ), filtRs = function ( str ){//Processing fields 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' />" + "<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 number </th>" + " <th class='uaddr'> User address </th>" + " <th class='phone'> User phone number </th>" + " <th class='number'> model </th>" + " <th class='money'> Collection of payments </th>" + " <th class='money'> Payment </th>" + " <th class='money'> Balance </th>" + " <th class='number'> Installer </th>" + " <th class='phone'> Sales phone number </th>" + " <th class='number'> Remark </th>" + " <th class='date'> Sales date </th>" + " <th class='date'> Record time </th>" + "</tr>"; while(!) { var id = num ,//filtRs(("id") ), uaddr = filtRs(("uaddr") ), uphone = filtRs(("uphone") ), typeid = filtRs(("typeid") ), received = filtRs(("received") ), prize = filtRs(("prize") ), unreceived = filtRs(("unreceived") ), installerid = filtRs(("installerid") ), sellerid = filtRs(("sellerid") ), remark = filtRs(("remark") ), selltime = $.(filtRs(("selltime") )), addtime = $.(filtRs(("addtime") ) ); html += "<tr jsselect='browzr_data'>" +"<td class='pid'>" + id + "</td>" +"<td class='uaddr'>" + uaddr + "</td>" +"<td class='phone'>" + uphone + "</td>" +"<td class='number'>" + typeid + "</td>" +"<td class='money'>" + received + "</td>" +"<td class='money'>" + prize + "</td>" +"<td class='money'>" + unreceived + "</td>" +"<td class='number'>" + installerid + "</td>" +"<td class='phone'>" + sellerid + "</td>" +"<td class='number'>" + remark + "</td>" +"<td class='date'>" + selltime + "</td>" +"<td class='date'>" + addtime + "</td>" +"</tr>"; // Statistics total_receive += received, total_prize += prize, num++; (); } html = html +"<tr class='total doNotFilter'>" +"<td class='pid'></td>" +"<td class='uaddr'>&Sigma; </td>" +"<td class='number'></td>" +"<td class='number'></td>" +"<td class='number'>" + total_receive +"</td>" +"<td class='number'>" + total_prize +"</td>" +"<td class='number'>" + (total_prize - total_receive ) +"</td>" +"<td class='number'></td>" +"<td class='number'></td>" +"<td class='number'></td>" +"<td class='date'></td>" +"<td class='date'></td>" +"</table>"; $.(rs); $.(); return html; }; = function(){ //Query lock if( typeof == 'undefined' ){ (false); } return ; }; = function( key ){ = key; }; //[[id],[type]] = [[ "uaddr", "uphone", "typeid", "received", "prize", "unreceived", "installerid", "sellerid", "remark", "selltime" ],[ 'text', 'text', 'num', 'num', 'num', 'num', 'num', 'num', 'text', 'date' ]]; = function(){ //Insert the installation order record var $ = roc, getV = $.( $. ); if(!getV){ $.('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 +')'; $.( sqlStr ); $.('Save the installation order successfully'); }; /*Show and Hide*/ = function( objDiv){ var $ = roc , cookieName = + 'cookie'; if( =='' || =='none' ){ $.( objDiv ); $.(cookieName,0,9999999); }else{ $.( objDiv ); $.(cookieName,1,9999999); } }; //Operation of cargo model = || {}; = [ ['tname','tprize','tdesc'], ['text','text','text'] ]; = function(){ //Insert record var $ = roc, getV = $.( $. ); if( !getV ){ $.('pop','Please fill in the information before saving! '); return; } var sqlStr = 'insert into type ( tname , tprize , tdesc ) values ('+ getV +')'; //$.('pop',sqlStr); $.( sqlStr ); $.('Save successfully! '); $.($.); }; = function( els ){ var $ = roc ; for(var i = 0 ; i < ; i ++){ var e = $.(els[i]+''); /* if( == 'input' && =='text'){*/ = ''; /*}*/ } }; //type{id,tname,tprize} = function( optId ){ //Get the type list var $ = roc , mySql = 'select * from type where isdel = 0 ', rs = $.( mySql ), filtRs = function ( str ){//Processing fields return (str+'')=='null' ? '':str; }, myOpt = $.( optId ), optIndex = 1; while(! ){ var id = filtRs(('id')), prize = filtRs(('tprize')), name = filtRs(('tname')); desc = filtRs(('tdesc')); [optIndex] = new Option( name , id ); [optIndex].title = 'price:' + prize + ' | Description:' + desc; optIndex++; (); } $.(rs); $.(); }; ;(function(){ var $ = roc ; $.("save").onclick = function(){ //save $.(); } $.("searchBtn").onclick = function(){ //Request inspection $.(); } //Initialize query installation form Year for(var i = 0 ; i <= 10 ; i++ ){ $.("s_year").options[i] = new Option(2010 + i , 2010 + i ); if( 2010+i+'' == (new Date()).getYear() ){ $.("s_year").options[i].selected = true; } } //Initialize query installation month for(var i = 1 ; i <= 12 ; i++ ){ $.("s_month").options[i] = new Option(i,i); } //Retrieve condition field id modification triggers 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]; $.(f).onpropertychange = function(){ if( == 'value'){ $.(true); if($.){ clearTimeout($.); } $. = setTimeout(function(){ $.(); },$.config.SELECT_DELAY); } } $.(f).onfocus = function(){ $.(this,"focusit"); }; $.(f).onblur = function(){ $.(this,"focusit"); }; } $.('saveType').onclick = function(){ //Cargo Type $.(); }; //Fetch the type list $.('typeid'); $.('s_style'); /* //$.("s_uaddr").onkeyup = $.("s_uphone").onkeyup = $.("s_style").onkeyup = function(){ $.("s_uaddr").onblur = $.("s_uphone").onblur = $.("s_style").onblur = function(){ $.(this,"focusit"); } $.("s_uaddr").onfocus = $.("s_uphone").onfocus = $.("s_style").onfocus = function(){ $.(this,"focusit"); }*/ //Switch navigation style for(var i = 0 ; i < $. ; i++ ){ var curObj = $.($.[0][i]+''); = function(){ $.(this,'focusit'); } = function(){ $.(this,'focusit'); } } //Add navigation click event var lis = $.('li',$.("ulNav")); for(var i = 0 ; i < ; i ++ ){ $.( $.(lis[i].id + 'Div')); lis[i].onclick = function(){ for(var n = 0 ; n < ; n ++ ){ $.(lis[n],'click'); $.( $.(lis[n].id + 'Div')); } $.( $.( + "Div")); $.(this,"click"); $.('showWhichDiv', ); } } //Default load display page var showWhichDiv = $.("showWhichDiv") || "searchList"; $.($.(showWhichDiv),"click"); $.($.(showWhichDiv + 'Div')); //Date control, thanks to the developer of this control for sharing, I 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 < ; i ++ ){ $.( numFields[i] ); } })();
I hope this article will be helpful to everyone's JavaScript programming.