本文实例讲述了Javascript连接Access数据库的方法。分享给大家供大家参考。具体实现方法如下:
var roc = roc || {}; roc.db = roc.db ||{}; //创建一个连接 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);//打开数据库 roc.db.conn = conn; return roc.db.conn; }; //获取连接 roc.db.getDb = function(){ if( roc.db.conn ){ return roc.db.conn; }else{ return roc.db.createDb(); } }; //关闭连接 roc.db.closeConn = function(){ if( roc.db.conn ){ roc.db.conn.close(); roc.db.conn = null; } }; //获取结果集 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; }; //关闭结果集 roc.db.closeRs = function( rs ){ rs.close(); rs =null; }; //更新、插入 roc.db.execute = function( sqlStr ){ var myConn = roc.db.getDb(); myConn.execute( sqlStr ); roc.db.closeConn(); }; /*---------Sigma:“我任你践踏我的尊严而毫不生气,是因为我爱你。”---------*/ 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 根据标签获取指定dom元素 * @param {String} tagName 元素标签名称 * @param {HTMLElement} el 元素所属的文档对象 默认为当前文档 * @return {HTMLElement} 返回HTMLElement元素 */ roc.dom.tagName = function(tagName, el) { var el = el || document; return el.getElementsByTagName(tagName); }; //删除左右两端的空格 roc.dom.trim = function (str) { return (str+'').replace(/(^\s*)|(\s*$)/g, ""); } /** * @method show 显示目标元素 * @param {Element} element 目标元素或目标元素的id * @param {String} element 目标元素 */ roc.dom.show = function (element) { element = roc.dom.id(element); element.style.display = ''; return element; }; /** * @method hide 隐藏目标元素 * @param {Element} element 目标元素或目标元素的id * @param {String} element 目标元素 */ roc.dom.hide = function (element) { element = roc.dom.id(element); element.style.display = 'none'; return element; }; /** * @method hasClass 判断元素是否含有 class * @param {Element} el 元素 * @param {String} className class 名称 */ roc.dom.hasClass = function(el, className){ var re = new RegExp('(^|\\s)' + className + '(\\s|$)'); return re.test(el.className); }; /** * @method addClass 给元素添加 class * @param {Element} el 元素 * @param {String} className class 名称 */ roc.dom.addClass = function(el, className){ if(!roc.dom.hasClass(el, className)){ el.className = el.className + ' ' + className; } }; /** * @method removeClass 给元素移除 class * @param {Element} el 元素 * @param {String} className class 名称 */ roc.dom.removeClass = function(el, className){ var re = new RegExp('(^|\\s)' + className + '(?:\\s|$)') el.className = el.className.replace(re, '$1'); }; /** * date对象命名空间 * * @namespace * @name data */ roc.date = roc.date || {}; /** * @method format 对目标日期对象进行格式化 * @param {Object} timestamp 目标日期对象 * @return {String} str 格式化后的时间 */ 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对象命名空间 * * @namespace * @name cookie */ roc.cookie = roc.cookie || {}; /** * @method set * @param {String} name cookie的键 * @param {String} value cookie的值 * @param {String} expires 失效时间(小时) * @param {String} domain domain域 * @param {String} path 路径 * @param {String} secure 是否支持https */ roc.cookie.set = function(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的键 */ 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 ){ //操作闪烁提示 var $ = roc , opt = $.dom.id("optTip"); $.util.toogle = $.util.toogle || 0; clearTimeout(roc.util.t);//调试 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 ){ //限制文本框、文本域只能输入数字 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 >= 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 ){ //批量获取表单值,用于插入 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(){ //组装搜索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 ', 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; }; //搜索 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("查询完毕");//闪烁 }; roc.search.getSel = function( sqlStr ){ //查询 var $ = roc, rs = $.db.getRs( sqlStr ), filtRs = function ( str ){//处理字段 return ( str + '' ) == 'null' ? '':str; }, num = 1; total_receive = 0, total_prize = 0, html = "
序号 | " + "用户地址 | " + "用户电话 | " + "型号 | " + "代收款 | " + "货款 | " + "余额 | " + "安装人 | " + "销售电话 | " + "备注 | " + "销售日期 | " + "记录时间 | " + "
---|---|---|---|---|---|---|---|---|---|---|---|
" + id + " | " +"" + uaddr + " | " +"" + uphone + " | " +"" + typeid + " | " +"" + received + " | " +"" + prize + " | " +"" + unreceived + " | " +"" + installerid + " | " +"" + sellerid + " | " +"" + remark + " | " +"" + selltime + " | " +"" + addtime + " | " +"
" +" | Σ | " +"" +" | " +" | " + total_receive +" | " +"" + total_prize +" | " +"" + (total_prize - total_receive ) +" | " +"" +" | " +" | " +" | " +" | " +" |
希望本文所述对大家的javascript程序设计有所帮助。