JavaScript – Working with Database

After reading this coding, you will know the following facts.

  1. How to connect the database in JavaScript
  2. How to use the ADODB.Recordset in JavaScript

As I already put the details comments in coding, I hope you will find it useful.

You can download the sourcecode and MSAccess database from this link.

http://michaelsync.net/demo/JavaScriptDatabaseSrc.zip

****

<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns=
"http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<script type
="text/javascript">
<!–
var adOpenDynamic = 2;
var adLockOptimistic = 3;

/* Path of database.
If you put the database "Inventory.mdb" in different location,
you need to specify the correct path to this variable.
But When you are running the webpage from Browsers only,
you should use this keyword "window.location.pathname" for getting
the current location. But If you are using FrontPage,
you hav to specity the static path.
*/
var strDbPath = "C:\\JavaScript Database\\Inventory.mdb";

/*
Here is the ConnectionString for Microsoft Access.
If you wanna use SQL or other databases, you hav
to change the connection string..
eg: SQL => var conn_str = "Provider=sqloledb; Data Source=itdev;" +
"Initial Catalog=pubs; User ID=sa;Password=yourpassword";
*/
var conn_str = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strDbPath;

function getAdoDb(strAdoType){
if (window.ActiveXObject){
return new ActiveXObject(strAdoType);
}
else{
return new ActiveXObject(strAdoType);
}
}

function showReports(){
try{
var strHtml ="";
strHtml += "<table cellpadding=0 cellspacing=0 border=1 width=500px align=center>";
strHtml += "<tr ><td align=center colspan=4><b>Stock List</b></td></tr>";

//Database Connection
var conn = getAdoDb("ADODB.Connection");
conn.open(conn_str, "", "");

//Recordset
var rs =
getAdoDb("ADODB.Recordset");
strQuery = "SELECT StockID,StockName,ReOrderLevel,IsActive FROM Stocks";
rs.open(strQuery, conn, adOpenDynamic, adLockOptimistic);

if(!rs.bof){
rs.MoveFirst();
while(!rs.eof) {
strHtml += "<tr>";
strHtml += " <td width=\"10px\">" + rs.fields(0).value + "</td>";
strHtml += " <td width=\"50px\">" + rs.fields(1).value + "</td>";
strHtml += " <td width=\"10px\">" + rs.fields(2).value + "</td>";
strHtml += " <td width=\"5px\">" + rs.fields(3).value + "</td>";
strHtml += "</tr>";

rs.MoveNext();
}
}
else{
//No Records.
strHtml += "<tr colspan=4><td align=center><font color=red>No Records.</font></td></tr>";
}
conn.close();
strHtml += "</table>";
document.write(strHtml);
}catch(ex){
alert(ex.message);
}
}

//–>
</script>
<title>
Stock List</title>
</head>
<!–<body onload="show_menu()">
<div id="main" />–>
<body>
<script language="JavaScript">
showReports();
</script>
</body>
</html>

149 thoughts on “JavaScript – Working with Database

  1. hi
    i am getting an error while opening the internet explorer page as no values givan for required parameters.i have connected html and access using java script.where i am doing wrong i could not get it.
    pls help me out

  2. Hi Alice, Sorry for very very late response.. I was so busy these days and couldn’t have that much time to take a look it.. actually, using ActiveX is not good one..

    Hi basanthi, using activex is not very good idea. So, Could you please do it with Ajax?

  3. hai
    I wrote the same code as above. Frontend is HTML, Backend is MS-access. we are having a group of linking forms,in first form data is storing in access and from second form data is not storing in access. Where am i going wrong Please help me.

  4. You should probably tell me what error you got or etc.. With your question, I’m not sure how I can help you.. The most of problems are already discussed in old comments so if you read each and every comments in this post, you may probably solve the problem… one more thing… I suggest not to use ActiveX object as much as possible.. Aax can do better..

  5. i wrote the code in the same format as above.
    when i enter values in form it is storing in database.And after completion of that file entry it is closingi.e.., the browser is collapsed.
    MY requirement is it should find the existing record and should enter the fields which are kept empty

  6. hi i got this error “ActiveX object not defined”…….
    How to rectify this error????????

  7. Hello,

    I want to create a very simple web application that will display the rows of a particular database table. However I also want the application to display new entries into the table, as they are inserted. Is there an easy way to determine when a new row has been inserted?

    Thanks.

  8. To Michael Sync,

    I have a program that I’ve created completely in javascript (html wrapped in an ebook exe program) that people love except for the fact that they need to complete all the tasks within my program to create their project; many of them have said they would like the ability to input some data and then return to the program and have the program automatically fill-in what data they inputted previously.

    I know cookies will not work, because they will not hold enough data; is it possible to have some kind of javascript database within the program that the software could auto write to and auto retrieve from.

    I’ve never worked with databases, but if this is something that can be done, I’m willing to pay for it.

    Thanks

  9. Hi,
    My Problem is that I would like to display all the data which are corresponding to the radio button values, but its not getting, I’m able to access it through the id’s but not through the values.

    Is there a way to populate this?

  10. Hi Michael,

    I know there is a long time to this artical , but realy I need your example
    In my application, I’m using js to connect a database. I tested your first code , but when I debugged the source code. A message poped up and said : ActiveXObject is not defined, also for my bad luck the files you have attached to reply Peifang don’t work.
    So please if you could send me this files.

    Thank you
    Rose

  11. hi Michael,
    You are really great.I was actually struggling for the last two weeks..in other forums ..i was told that we need to use server side scripting even when db is located locally..great effort..and effort appreciated.

  12. i want to check onblur of a text box.when i click outside the text box.that time it will check in database filled value is existing in database or not…for that what can i do?

  13. Hi Mike,
    I was able to run this code locally on windows platform, but when I put it into the server, It didn’t work. I am getting the following message
    “the connection cannot be used to perform this operation. It is either closed on invalid in this context”.

    Does the connection string need to be specified differently ? I updated the path for the var strDbPath (used /folder/file.mdb), but kept everything else same for var conn_str.
    var conn_str = “Provider=Microsoft.Jet.OLEDB.4.0;Data Source=” + strDbPath.

    Would you know how to resolve this error. I can be reached at yiu51car@yahoo.com

  14. Hi,

    I found this code and it is working great, when the path to file is specified on an Harddisk. But how can I get this code working with a database placed on the server, e.g. http://myserver/mydatabase/database.mdb?

    Anyone who got this working?

    Any help would be appreciated.

    Best Regards,
    Klaus.

  15. Hi,

    I tried the code and it is working fine as long as the .mdb is specified with an absolute path. But when I try to use only the Database name, because it is located in the same folder as the .htm or when I try to use the Intranetlink it does not work. Does this not work at all or am I doing something wrong?

    Best Regards,
    Klaus.

  16. Sorry, for posting again. I checked this site again and did not find my comment. So I thought maybe something went wrong.

    I am really sorry.

  17. Hi Michael Sync,

    I saw your code and its working fine. Is it possible to fetch a stored image from ms-access using this ajax script. Does i have to include any addition for displaying image because i tried but i was unable to get it. Help me.

    Regards
    V.Sethu

  18. i run this code but there’s an error occur when i open the page. the error is “ActiveXObject not defined”. what should be the problem of this?

    can u fix it?

  19. hi michael, i dont get output from the code above, can you make it step by step, i need it for my project. thank you for helping . more powers

  20. Hi Michael,

    I tried ur code its really working fine .Justed wanted to say thanks for sharing ur knowledge with whole world.

    U ROCK :)

  21. Why are Microsoft programmers so dumb? This is such a trivial task, and yet many of you are struggling.

  22. hi i just need to do a mini project using html javascript and also connect with database.
    plz send me some source code with outputs screenshots so that it will help me

  23. I am a professional programmer, but this is the first time I have to connect Javascript to an MDB on the client side.

    I downloaded your example, but received this message: ActiveXObject is not defined.

    I am using WinXP, Access 2000, Firefox.

  24. hi i tried so many code but i can not connect .mdb file with java script or may be i never know how to connect it so, please help me.

  25. this sample is executed on xp but it can not execute on window 7 64 bit os. can u suggest some thing plz!!!!

  26. sir ,when i open the webpage than i have faced a problem to “java script alert:can not define java script parameter”

    plz solve the problem thanks.

  27. Hi,

    I tried this but could it be that it doesn’t work in IE on Windows 8?
    I have the warning that it hasn’t the security rights to open the file?

  28. Howdy! I understand this is kind of off-topic however I
    needed to ask. Does building a well-established website like yours require a large amount of work?
    I’m completely new to running a blog however I do
    write in my journal everyday. I’d like to start a blog so I will be able to share my own experience and
    thoughts online. Please let me know if you have any ideas
    or tips for brand new aspiring blog owners. Thankyou!

  29. hello!,I really like your writing very much! percentage we keep up a correspondence extra about your
    post on AOL? I require a specialist in this area to
    solve my problem. Maybe that is you! Taking a look forward
    to peer you.

  30. Excellent site. A lot of helpful info here. I’m sending it to several friends ans
    additionally sharing in delicious. And of
    course, thank you in your sweat!

  31. Excellent post. I used to be checking constantly this weblog and I’m inspired!
    Very useful information specially the final phase :) I take care of
    such information a lot. I was looking for this particular information for a
    very long time. Thank you and good luck.

Leave a Reply

Your email address will not be published. Required fields are marked *