Link to home
Start Free TrialLog in
Avatar of rac13
rac13

asked on

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
Avatar of hongjun
hongjun
Flag of Singapore image

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
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
Avatar of rac13
rac13

ASKER

Wat abot vbscript then?
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
Avatar of rac13

ASKER

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?
I don't think that's possible

hongjun
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.
Avatar of rac13

ASKER

I'm quite interested in the applets one. I did try before, but my applets are not loading...
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?
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?
Avatar of rac13

ASKER

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.

Avatar of rac13

ASKER

I'm a beginner.
ASKER CERTIFIED SOLUTION
Avatar of setien
setien

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial

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/
Avatar of rac13

ASKER

I can't load my page. It said that the class is not found. I haven't get to the javascript yet.
Avatar of rac13

ASKER

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.
Are you using applet html that visual cafe is generating?
It sounds like you are just leaving out codebase="..." from your applet tag?
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 :)
Avatar of rac13

ASKER

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.
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.
Avatar of rac13

ASKER

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.
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".

Avatar of rac13

ASKER

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.
I'm fresh outta options then.
Avatar of rac13

ASKER

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.
how doesn't it work?
Do you have the proper objects and methods visible to javascript?
what kind of error do you get?
Avatar of rac13

ASKER

Run time error:
'this.MFGList.cbMFG' is not an object.
Avatar of rac13

ASKER

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.
Cool... you are getting there :)
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.