Read from Database using Javascript?

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
rac13Asked:
Who is Participating?
 
setienCommented:
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
 
hongjunCommented:
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
 
hongjunCommented:
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
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
rac13Author Commented:
Wat abot vbscript then?
0
 
hongjunCommented:
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
 
rac13Author Commented:
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
 
hongjunCommented:
I don't think that's possible

hongjun
0
 
setienCommented:
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
 
rac13Author Commented:
I'm quite interested in the applets one. I did try before, but my applets are not loading...
0
 
setienCommented:
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
 
setienCommented:
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
 
rac13Author Commented:
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
 
rac13Author Commented:
I'm a beginner.
0
 
setienCommented:

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
 
rac13Author Commented:
I can't load my page. It said that the class is not found. I haven't get to the javascript yet.
0
 
rac13Author Commented:
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
 
setienCommented:
Are you using applet html that visual cafe is generating?
It sounds like you are just leaving out codebase="..." from your applet tag?
0
 
setienCommented:
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
 
rac13Author Commented:
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
 
setienCommented:
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
 
rac13Author Commented:
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
 
setienCommented:
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
 
rac13Author Commented:
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
 
setienCommented:
I'm fresh outta options then.
0
 
rac13Author Commented:
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
 
setienCommented:
how doesn't it work?
Do you have the proper objects and methods visible to javascript?
what kind of error do you get?
0
 
rac13Author Commented:
Run time error:
'this.MFGList.cbMFG' is not an object.
0
 
rac13Author Commented:
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
 
setienCommented:
Cool... you are getting there :)
0
 
DeathwishCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.