Home >Web Front-end >JS Tutorial >How to connect to access database using javascript_javascript skills

How to connect to access database using javascript_javascript skills

WBOY
WBOYOriginal
2016-05-16 19:24:101344browse

I just saw a program written by a friend that uses javascript to connect to an excel database. I wanted to change it to an access database, so I found these two articles
———————————————————————— ----------------------------
I have spent a lot of effort in the past few days to find ways to connect javascript to the access database on the Internet,
After sorting and summarizing, I finally wrote a piece of code that can be effectively executed.

But this code can run normally locally, but once it is placed on a free personal space, it cannot be executed. The main reason is that the operation permissions are insufficient, because most free spaces that only support static pages
has made very strict restrictions, so whether it is using mdb or reading and writing plain text files,
cannot pass, and a js error will be reported by the page. Some free spaces abroad directly prohibit the uploading of files with extensions like mdb
.

Although there is no hope of making your own counter or message board on the free space of purely static pages,
this program may still be useful to some friends, so I wrote it for you to study.

File composition:
The Access database is named MyData.Mdb, and a table named count is built in it. The table consists of two
fields: ID and COUNT, and there is a piece of data in the table: ('count','100').
The text file is called count.txt, and any number is written in it.
The static page is named Cnt.htm.
The above 3 files are placed in the same directory.

Since the Access database is used, and it uses non-standard SQL syntax, please pay attention to the need to add square brackets to the table name
and field name: []

The following is from the page Code:

The getCountFromDB method operates on the Access database, and the getCountFromTxt
method operates on txt plain text. Both methods are executed locally.


Copy code The code is as follows:



New Document < ;/TITLE> <br><META NAME="Generator" CONTENT="EditPlus"> <br><META NAME="Author" CONTENT=""> <br><META NAME="Keywords" CONTENT=""> <br><META NAME="Description" CONTENT=""> <br><SCRIPT LANGUAGE="JavaScript"> <br><!-- <BR>function getCountFromDB( ) { <BR> //Based on the current page file, find the absolute path where the file is located. <BR> var filePath = location.href.substring(0, location.href.indexOf("Cnt.htm")); <BR> var path = filePath "MyData.mdb"; <BR> //Remove the string The first 8 characters of "files://". <BR> path = path.substring(8); <BR> var updateCnt = 0; <br><br> //Generate sql statements for query and update. <BR> var sqlSelCnt = "Select COUNT FROM [COUNT] Where ID = 'count'"; <BR> var sqlUpdCnt = "Update [COUNT] SET [COUNT] = '"; <br><br> //Establish a connection , and generate the relevant string www.knowsky.com. <BR> var con = new ActiveXObject("ADODB.Connection"); <BR> con.Provider = "Microsoft.Jet.OLEDB.4.0"; <BR> con.ConnectionString = "Data Source=" path; <br> <br> con.open; <BR> var rs = new ActiveXObject("ADODB.Recordset"); <BR> rs.open(sqlSelCnt, con); <BR> while (!rs.eof) { <BR> var cnt = rs.Fields("COUNT"); <BR> document.write(cnt); <BR> //Add 1 to the result and update the database. <BR> updateCnt = cnt * 1 1; <BR> rs.moveNext; <BR> } <BR> rs.close(); <BR> rs = null; <br><br> sqlUpd Cnt = sqlUpdCnt updateCnt "'" ; <BR> con.execute(sqlUpdCnt); <br><br> con.close(); <BR> con = null; <BR>} <br><br>function getCountFromTxt() { <BR> var filePath = location.href.substring(0, location.href.indexOf("Cnt.htm")); <BR> var path = filePath "count.txt"; <BR> path = path.substring(8); <BR> var nextCnt = 0; <br><br> var fso, f1, ts, s; <BR> //Open the text file in read-only mode. <BR> var ForReading = 1; <BR> //Open the text file in reading and writing mode.<BR> var ForWriting = 2; <br><br> fso = new ActiveXObject("Scripting.FileSystemObject"); <BR> f1 = fso.GetFile(path); <BR> ts = f1.OpenAsTextStream(ForReading, true ); <BR> s = ts.ReadLine(); <BR> nextCnt = eval(s) 1; <BR> document.write("now count is:" s); <BR> ts.Close(); <br><br> ts = f1.OpenAsTextStream(ForWriting, true); <BR> ts.WriteLine(nextCnt); <BR> ts.close(); <BR>} <BR>//--> <br></SCRIPT> <br></HEAD> <br><br><BODY> <br><SCRIPT LANGUAGE="JavaScript"> <br><!-- <BR>getCountFromTxt( ); <BR>//--> <br></SCRIPT> <br></BODY> <br></HTML></div> <br><br>Postscript: Although only The free space for static pages is not available, but I think some free space that supports <br>Asp should be available. Of course, I have not tried it specifically, I am just imagining it. <br>And using this method to read and write databases is only suitable for work such as counters and message boards that do not require <br> confidentiality. After all, the code is all written in javascript and anyone can see it. <br>Then you can get the path to download the db file for analysis, so you can’t use it to <br>save things you don’t want others to know. <br>As one of my colleagues said, using javascript to connect to the database for operations is simply a step forward. Haha, it is true. Since you cannot use free space to make your own counter, Then this code can only be used for fun and to find a good feeling for yourself. <br>-------------------------------------------------<br>Many friends online feel that when they are solving certain problems, I am very confused, and many people find it difficult to understand how to use JavaScript to connect to the database. They even conclude that JavaScript can only be executed by the client and cannot connect to the database. And I searched it and found that the introduction on CSDN is too general for beginners. I can't understand it at all. I didn't search other forums. In this situation, I decided to take a good look at MSDN. After looking at VC's examples of connecting to the database, I summarized the method of connecting to the database using JAVASCRIPT. I now publish it to everyone <br><br><br><br><div class="codetitle"> <span>Copy code<a style="CURSOR: pointer" data="75002" class="copybut" id="copybut75002" onclick="doCopy('code75002')"><u></u> The code is as follows:</a></span><script language=javascript> </div> <div class="codebody" id="code75002">// ^_^ <br><br>// <br><br>//(c) in 2002-2003 by Alex(Chinese: Jiang Jianhua) <br><br>//http://www.finder.net.cn <br><br>//C Email : jiangjh@finder.net.cn <br><br>//F Email: jjh_115@eyou.com <br><br></script> <br><br><script language=javascript> <br><br>function replace(v) <br><br>{ <br><br>//Fault tolerance issues, please make your own judgment. <br><br>//Define SQL statement <br><br>var sql = "select * from Dictionary where MainID='" v "'" ; <br><br>//Create a new database connection object and data set Access object <br><br>var ConnDB = new ActiveXObject("adodb.connection"); <br><br>var rs = new ActiveXObject("ADODB.Recordset"); <br><br>//Here Fill in the DSN to be connected <br><br>ConnDB.ConnectionString="DSN=***;uid=***;pwd=***"; <br><br>ConnDB.open <br><br>rs.open(sql,ConnDB,1,1); <br><br>if (rs.RecordCount>0){ <br><br>if (v==parseInt(rs("MainID"))) { <br><br>//String connection <br><br>document.all('txt').value = v rs("MainValue"); <br><br>var sql=null; <br><br>rs.close <br><br>ConnDB.close <br><br>return; <br><br>} else { <br><br>var sql=null; <br><br> alert("No such codename!"); <br><br>txt.select(); <br><br>txt.focus(); <br><br>rs.close <br><br>ConnDB .close <br><br>} <br><br>} <br><br></script> <br><br><input type=text id=txt name=txt onblur="replace( this.value)"> <br> </div> </div></div><div class="nphpQianMsg"><div class="clear"></div></div><div class="nphpQianSheng"><span>Statement:</span><div>The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn</div></div></div><div class="nphpSytBox"><span>Previous article:<a class="dBlack" title="JavaScript Function in detail_Basic knowledge" href="https://m.php.cn/faq/26912.html">JavaScript Function in detail_Basic knowledge</a></span><span>Next article:<a class="dBlack" title="JavaScript Function in detail_Basic knowledge" href="https://m.php.cn/faq/26914.html">JavaScript Function in detail_Basic knowledge</a></span></div><div class="nphpSytBox2"><div class="nphpZbktTitle"><h2>Related articles</h2><em><a href="https://m.php.cn/article.html" class="bBlack"><i>See more</i><b></b></a></em><div class="clear"></div></div><ins class="adsbygoogle" style="display:block" data-ad-format="fluid" data-ad-layout-key="-6t+ed+2i-1n-4w" data-ad-client="ca-pub-5902227090019525" data-ad-slot="8966999616"></ins><script> (adsbygoogle = window.adsbygoogle || []).push({}); </script><ul class="nphpXgwzList"><li><b></b><a href="https://m.php.cn/faq/1609.html" title="An in-depth analysis of the Bootstrap list group component" class="aBlack">An in-depth analysis of the Bootstrap list group component</a><div class="clear"></div></li><li><b></b><a href="https://m.php.cn/faq/1640.html" title="Detailed explanation of JavaScript function currying" class="aBlack">Detailed explanation of JavaScript function currying</a><div class="clear"></div></li><li><b></b><a href="https://m.php.cn/faq/1949.html" title="Complete example of JS password generation and strength detection (with demo source code download)" class="aBlack">Complete example of JS password generation and strength detection (with demo source code download)</a><div class="clear"></div></li><li><b></b><a href="https://m.php.cn/faq/2248.html" title="Angularjs integrates WeChat UI (weui)" class="aBlack">Angularjs integrates WeChat UI (weui)</a><div class="clear"></div></li><li><b></b><a href="https://m.php.cn/faq/2351.html" title="How to quickly switch between Traditional Chinese and Simplified Chinese with JavaScript and the trick for websites to support switching between Simplified and Traditional Chinese_javascript skills" class="aBlack">How to quickly switch between Traditional Chinese and Simplified Chinese with JavaScript and the trick for websites to support switching between Simplified and Traditional Chinese_javascript skills</a><div class="clear"></div></li></ul></div></div><ins class="adsbygoogle" style="display:block" data-ad-format="autorelaxed" data-ad-client="ca-pub-5902227090019525" data-ad-slot="5027754603"></ins><script> (adsbygoogle = window.adsbygoogle || []).push({}); </script><footer><div class="footer"><div class="footertop"><img src="/static/imghwm/logo.png" alt=""><p>Public welfare online PHP training,Help PHP learners grow quickly!</p></div><div class="footermid"><a href="https://m.php.cn/about/us.html">About us</a><a href="https://m.php.cn/about/disclaimer.html">Disclaimer</a><a href="https://m.php.cn/update/article_0_1.html">Sitemap</a></div><div class="footerbottom"><p> © php.cn All rights reserved </p></div></div></footer><script>isLogin = 0;</script><script type="text/javascript" src="/static/layui/layui.js"></script><script type="text/javascript" src="/static/js/global.js?4.9.47"></script></div><script src="https://vdse.bdstatic.com//search-video.v1.min.js"></script><link rel='stylesheet' id='_main-css' href='/static/css/viewer.min.css' type='text/css' media='all'/><script type='text/javascript' src='/static/js/viewer.min.js?1'></script><script type='text/javascript' src='/static/js/jquery-viewer.min.js'></script><script>jQuery.fn.wait = function (func, times, interval) { var _times = times || -1, //100次 _interval = interval || 20, //20毫秒每次 _self = this, _selector = this.selector, //选择器 _iIntervalID; //定时器id if( this.length ){ //如果已经获取到了,就直接执行函数 func && func.call(this); } else { _iIntervalID = setInterval(function() { if(!_times) { //是0就退出 clearInterval(_iIntervalID); } _times <= 0 || _times--; //如果是正数就 -- _self = $(_selector); //再次选择 if( _self.length ) { //判断是否取到 func && func.call(_self); clearInterval(_iIntervalID); } }, _interval); } return this; } $("table.syntaxhighlighter").wait(function() { $('table.syntaxhighlighter').append("<p class='cnblogs_code_footer'><span class='cnblogs_code_footer_icon'></span></p>"); }); $(document).on("click", ".cnblogs_code_footer",function(){ $(this).parents('table.syntaxhighlighter').css('display','inline-table');$(this).hide(); }); $('.nphpQianCont').viewer({navbar:true,title:false,toolbar:false,movable:false,viewed:function(){$('img').click(function(){$('.viewer-close').trigger('click');});}}); </script></body></html>