Solved

Read from Database using Javascript?

Posted on 2001-08-02
30
613 Views
Last Modified: 2006-11-17
Hi,
  is it possible for the javascript to call and read from a database from a server and load it into the html? If possible, where can I find the sample code?

Thanks
0
Comment
Question by:rac13
  • 13
  • 12
  • 4
  • +1
30 Comments
 
LVL 33

Expert Comment

by:hongjun
ID: 6347174
It is impossible to do that. JavaScript is a client side language and thus is not capable of doing that. If you really do wish to read data from a database table, then I suggest you using a server-side language like Active Server Pages, ColdFusion, etc.


hongjun
0
 
LVL 33

Expert Comment

by:hongjun
ID: 6347178
How to do that in ASP?
http://www.asp101.com/samples/database.asp


When using ASP, you would need a web server like PWS or IIS.

When using ColdFusion, you would need a web server like IIS or PWS. Apart from that, you would also need ColdFusion server.

hongjun
0
 

Author Comment

by:rac13
ID: 6347199
Wat abot vbscript then?
0
 
LVL 33

Expert Comment

by:hongjun
ID: 6347334
You cannot do that in client side. Must do it in server side. Use ASP if you are familiar with VBScript since a server codes can be in VBScript.

hongjun
0
 

Author Comment

by:rac13
ID: 6347338
I had this constrait that my page had to be in a html, is there anyway that I can call a servlet and display it in my html?
0
 
LVL 33

Expert Comment

by:hongjun
ID: 6347340
I don't think that's possible

hongjun
0
 

Expert Comment

by:setien
ID: 6347643
Actually I do believe there is a way to access a database without using a serverside language. There are several options, but you can't do it with pure ECMA compliant JavaScript.

Depending how elegant or clumsy you want to do it, the browsers versions it must be compatible with, and how much control you have over your DB server, there are three options:

1) Put an applet in a hidden frame, and have that look up the database values and make them available to your html in another frame. This should work in any proper java enabled browser.
This will work with pretty much any normal database.

2) Put an applet in the actual page that you want to show the data, have it look up the data, and modify the page on the fly, with w3cs set of DOM javascript functions, and the "innerHTML" property, which will work in both Netscape 6 and above, and Explorer 5.0 and above.
This will work with pretty much any normal database.

3) Put a flash element on your page, and have that show your data instead. Flash 5 can create XMLSockets that connect to a server and communicate XML messages back and forth. If you can make sure your database server speaks XML, then you can get the data like that. Flash CAN actually communicate the data to JavaScript as well, but that's getting a bit to kludgy.

Tell me if you are interested in any of these solutions, in which case I will answer the one you want. You will have to up the number of points though - the descriptions will take some time.
0
 

Author Comment

by:rac13
ID: 6347655
I'm quite interested in the applets one. I did try before, but my applets are not loading...
0
 

Expert Comment

by:setien
ID: 6347670
Start a new question for more points called "how do I use applets to lookup data in a database, and present them in HTML.

Which applets one are you interested in?
0
 

Expert Comment

by:setien
ID: 6347677
I should probably ask you what your level of Java expertise is, before embarking on an explanation of this scheme.

What is your level of java expertise?
0
 

Author Comment

by:rac13
ID: 6347682
This method:
2) Put an applet in the actual page that you want to show the data, have it look up the data, and modify
the page on the fly, with w3cs set of DOM javascript functions, and the "innerHTML" property, which
will work in both Netscape 6 and above, and Explorer 5.0 and above.
This will work with pretty much any normal database.

If the solution works, I'll increased the points. Don't worry, you'll get ur expert points.

0
 

Author Comment

by:rac13
ID: 6347684
I'm a beginner.
0
 

Accepted Solution

by:
setien earned 10 total points
ID: 6347843
Since I have a job to tend to, I can't offer you a cut/paste solution for this. You will have to do some studying on your own. It's simply too time consuming to explain applet programming, java-database programming and such topics here.

What you need to do is this :

1) MySQL access from applet
Study up on mysql (a guess) and java - if you are using another db, then find some material on it. There should be tonnes of examples for practically any db.
suggested starting point :
http://www.artima.com/java/answers/Sep2000/messages/77.html

Once you know how to attain data from a database in java, go to point 2.

2) Basic HTML layout and adding applet.
Make your basic html layout in an editor or by hand. You might want to use some pregenerated applet-html from your compiler (if you are using something like MC J++ or IBM Visual Age for java).

3) Java -> JavaScript communication
Read up on :
http://www.codeproject.com/jscript/javatojs.asp
This should explain what you need to know about getting the data from Java to JavaScript.

4) DOM JavaScript Bindings
Once you have your applet embedded in the page, you will want to go and have a look at :
http://www.w3.org/TR/2001/WD-DOM-Level-3-Core-20010605/ecma-script-binding.html

This page is about how to use javascript to alter the contents of a document by means of javascript.
There is an example of how you can use it at :
http://www.mindflow.dk/get/dyntest.html

Each function is not necessarily implemented in both IE5 and NS6, but you can run tests at :
http://www.jeremie.com/Dev/DOM/index.jer
which will reveal what function do and do not work in ns and IE.
The dyntest.html example I made is also cross-browser, and contains all that you need.

Now you have the knowledge you need to make it work :
- How to get data from the database into an applet in the browser
- How to get that data into JavaScript
- How to modify the page with javascript.

Now get going :) Good luck.
0
 

Expert Comment

by:setien
ID: 6347846

Depending on how much of a beginner you are with java, you might want to take a look at the excellent :

http://java.sun.com/docs/books/tutorial/
0
 

Author Comment

by:rac13
ID: 6354923
I can't load my page. It said that the class is not found. I haven't get to the javascript yet.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:rac13
ID: 6355128
It is working well in the Visual Cafe, but once I cab in and load onto my html page..

It said that it can't load.
0
 

Expert Comment

by:setien
ID: 6355137
Are you using applet html that visual cafe is generating?
It sounds like you are just leaving out codebase="..." from your applet tag?
0
 

Expert Comment

by:setien
ID: 6355141
May I ask why you you are so set on making it client side ?
It really is much easier doing it serverside?

It's a much bigger challenge to do it clientside though :)
0
 

Author Comment

by:rac13
ID: 6355163
This is my HTML:

<HTML>
<HEAD>
<TITLE>QUEQPPRICEPage</TITLE>
</HEAD>

<body bgcolor="#C0C0C0" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
<center><table><tr><td bgcolor="#CC0000" align="center" valign="middle">
<font face="Arial, Helvetica, sans-serif" size="5" color="#FFFFFF">
<b><i> Corporate Customers' Equipment Pricing Enquiry  </i></b></font>
</td</tr></table></center>

<table><tr><td><font size="+1"><b>Please Select a Manufacturer: </b></td>
<td>
<APPLET CODE="QUEQPPRICE" Name="MFGList" WIDTH=200 HEIGHT=200>
<param name="namespace" value="Eqpt Price">
<param name=useslibrary value="Eqpt Price (Dev)">
<param name=useslibrarycodebase value="/NetDynamics/applets/eqptprice.cab">
<param name=useslibraryversion value="0,0,0,2">
</APPLET>
</td></tr>
<tr><td><center><input type="submit" value="Fetch Details"></center></td></tr>
</table>

</BODY>
</HTML>

I'm not sure wat this application is, but it seems that they only can have one HTML file. If I have the choice I would do otherwise too.
0
 

Expert Comment

by:setien
ID: 6355220
That's makes no sense to me out of context. You'll have to read up on the applet tag. Your applet is using some kind of class that it cant find, I think. That's usually due to the applettag.

You might try and stick a codebase="." into the applet tag and see if that solves it.
0
 

Author Comment

by:rac13
ID: 6355227
They are using Net Dynmaic to do this application. The html page is created by it. Then I have to modify the page to display wat I want.

I don't get wat u mean.
0
 

Expert Comment

by:setien
ID: 6355245
Change :
<APPLET CODE="QUEQPPRICE" Name="MFGList" WIDTH=200 HEIGHT=200>

to :
<APPLET CODE="QUEQPPRICE" Name="MFGList" WIDTH=200 HEIGHT=200 codebase=".">


I can't explain it any clearer. Go to http://java.sun.com and read about the applet tag under "documentation".

0
 

Author Comment

by:rac13
ID: 6357783
I had tried your suggestion but it won't work. I had also check the .cab file and all the class that I need are in there.
0
 

Expert Comment

by:setien
ID: 6358379
I'm fresh outta options then.
0
 

Author Comment

by:rac13
ID: 6363006
How should I get the data from the applet using Javascript?

<HTML>
<HEAD>
<TITLE>QUEQPPRICEPage</TITLE>
<script language = "JavaScript">
function getMFG()
{
var MFG = this.MFGList.cbMFG.getSelectedItem();
alert("The selected Item: " + MFG);
}
</script>
</HEAD>

<body bgcolor="#C0C0C0" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
<form name="QUCORPPLANPage" method="POST" ACTION="" onSubmit="getMFG()">
<center><table><tr><td bgcolor="#CC0000" align="center" valign="middle">
<font face="Arial, Helvetica, sans-serif" size="5" color="#FFFFFF">
<b><i> Corporate Customers' Equipment Pricing Enquiry  </i></b></font>
</td</tr></table></center>
<br><br>
<table><tr><td><font size="+1"><b>Please Select a Manufacturer: </b></td>
<td>
<APPLET ARCHIVE="/NetDynamics/applets/etprice.jar" CODE=EqutPrice NAME
="MFGList" WIDTH=200 HEIGHT=50>
</APPLET>
</td></tr>
<tr><td><center><input type="submit" value="Fetch Details"></center></td></tr>
</table>
</body>
</html>

The Javascript don't work.
0
 

Expert Comment

by:setien
ID: 6363031
how doesn't it work?
Do you have the proper objects and methods visible to javascript?
what kind of error do you get?
0
 

Author Comment

by:rac13
ID: 6363068
Run time error:
'this.MFGList.cbMFG' is not an object.
0
 

Author Comment

by:rac13
ID: 6363235
This is my new HTML and it is working:

<HTML>
<HEAD>
<TITLE>QUEQPPRICEPage</TITLE>
</HEAD>

<body bgcolor="#C0C0C0" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">

<form name="QUCORPPLANPage" method="POST" ACTION="http://localhost/servlet/CorpEqptEnquir
yServlet_Action" onSubmit="getMFG()">
<center><table><tr><td bgcolor="#CC0000" align="center" valign="middle">
<font face="Arial, Helvetica, sans-serif" size="5" color="#FFFFFF">
<b><i> Corporate Customers' Equipment Pricing Enquiry  </i></b></font>
</td</tr></table></center>
<br><br>
<table><tr><td><font size="+1"><b>Please Select a Manufacturer: </b></td>
<td>
<APPLET ARCHIVE="/NetDynamics/applets/etprice.jar" CODE=com.singtel.EasiQUEQPPRICE.EqutPrice NAME
="MFGList" WIDTH=200 HEIGHT=50>
</APPLET>
</td></tr>
<tr><td><center><input type="submit" value="Fetch Details"></center></td></tr>
</table>
<script language = "JavaScript">
function getMFG()
{
document.write('<input type="hidden" name="MFG" value="' + document.MFGList.getListItem() + '">'
);
}
</script>
</form>

</BODY>
</HTML>

But I am unable to get my servlet working.
It gets the error as below:

500 Internal Server Error
/servlet/CorpEqptEnquiryServlet_Action:

null
java.lang.NullPointerException
     at CorpEqptEnquiryServlet_Action.doPost(CorpEqptEnquiryServlet_Action.java:36)
     at javax.servlet.http.HttpServlet.service(HttpServlet.java:772)
     at javax.servlet.http.HttpServlet.service(HttpServlet.java:865)
     at allaire.jrun.servlet.JRunSE.service(JRunSE.java:1013)
     at allaire.jrun.servlet.JRunSE.runServlet(JRunSE.java:925)
     at allaire.jrun.servlet.JRunNamedDispatcher.forward(JRunNamedDispatcher.java:34)
     at allaire.jrun.servlet.Invoker.service(Invoker.java:84)
     at allaire.jrun.servlet.JRunSE.service(JRunSE.java:1013)
     at allaire.jrun.servlet.JRunSE.runServlet(JRunSE.java:925)
     at allaire.jrun.servlet.JRunRequestDispatcher.forward(JRunRequestDispatcher.java:88)
     at allaire.jrun.servlet.JRunSE.service(JRunSE.java:1131)
     at allaire.jrun.servlet.JvmContext.dispatch(JvmContext.java:330)
     at allaire.jrun.http.WebEndpoint.run(WebEndpoint.java:107)
     at allaire.jrun.ThreadPool.run(ThreadPool.java:272)
     at allaire.jrun.WorkerThread.run(WorkerThread.java:75)


But when I got the Javascript out and manually put in the hidden value. It works.
0
 

Expert Comment

by:setien
ID: 6363246
Cool... you are getting there :)
0
 
LVL 2

Expert Comment

by:Deathwish
ID: 6420465
There are two main ways I have worked with that have worked well in achieving what you want. Both depend on you using a IIS or PWS situation. The first is one if you are using a all IE situation. Its called RDS. Go http://www.rdsdeveloper.com/ASPTutorial.html for details on it. Once setup properly you access data via ado commands and such. To access databases you have to setup a DSN on the server. By using a DSN you can pretty much query any db from there. I have done it with SQL 7 and MySQL.

If in a multibrowser situation, you can then use another method that does use a hidden java applet. It again needs IIS or PWS, but it does work in Netscape. Its called Remote Scripting. It basically sends a request to an .asp page that then executes and can send back data. It can send back delimited data or if using IE only ado objects. I have used to send back text delimited data and then split it out and use it accordingly. The cool thing about it is that since it queries a asp page you do anything in the asp page such as com objects and such to retrieve and do calculations. Here is the link on the MS site for it:

http://msdn.microsoft.com/scripting/remotescripting/rsdocs.htm

I have used it wilt Netscape 4.x. I can't remember if I have tried it with version 6 netscape. It works for sure in IE.

As I said this works if your webserver is MS.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now