Michael Sync

Michael Sync

Crisp, minimal personal blog

14 Jun 2006

JavaScript – Working with Database


A lot of junior developers or students used to ask this question “Is it possible to connect the database from Javascript?”. The answer is that you can if your browser or OS supports it. but it’s not the recommended.

If you insist that you have the unique requirement (e.g. just for learning) and the security is not your concern, this post will show you how to do it.

Source Code: JavaScriptDatabaseSrc.zip

You can download the source code from my github repo. I put the comments in the code.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<!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>
      <script language="JavaScript">
         showReports();
      </script>
   </body>
</html>