Problem in Accessing DataBase using an applet

Dear All,

I am accessing a database using an applet but it's not working.

plz help.

The code is enlcosed herewith.

The code is enclosed herewith

function enblLicense()

{

//alert(document.forms[0].subject.value+" "+document.forms[0].subject.selectedIndex);

var Sub_Code = document.forms[0].subject.value;

var Wpn_Code = new Array();

var app=document.applets[0];

n=0;

for ( var w=0; w<=document.forms[0].WeaponType.length-1; w++)

{

var lstObj = document.forms[0].WeaponType;

if(lstObj)

{

if(lstObj.options[w].selected == true)

{

Wpn_Code[n] = lstObj.options[w].value;

n++;

}

}

}

for( var j=0; j<= Wpn_Code.length-1; j++)

{

alert("value " + Wpn_Code[j] );

}

var str = "SELECT Govt_Fee, Service_Fee FROM tblCharges WHERE (Subject_Code =" + Sub_Code + " AND ";

for(i=0; i<Wpn_Code.length; i++)

{

str = str +" OR " + " WeaponCode= " + Wpn_Code ;

}

str = str + ")";

alert(str);

var st=app.execute("str","alis");

//alert("String " + st)

var i=0;

var feeg=0;

while (app.next())

{

i++;

feeg=feeg+(app.getString(1)*1);

alert("feeg" + feeg);

}

}>

[1331 byte] By [aarohan_jamwala] at [2007-11-26 22:50:25]
# 1
there's nothing there to access a database, no wonder it's not working...And even if there was it wouldn't work because applets have no access to external resources.
jwentinga at 2007-7-10 12:11:38 > top of Java-index,Java Essentials,Java Programming...
# 2
Not to mention that it doesn't even seem to be Java code. It looks like Javascript. I suppose you're passing the SQL query to an applet and hoping that the applet will query the DB.Do you realize how sick this is?
paulcwa at 2007-7-10 12:11:38 > top of Java-index,Java Essentials,Java Programming...
# 3

Hello paulcw ,

yeah i am passing the sql query to an applet and hoping that the applet will query the DB.

var app1 =document.applets[0];

var LicNo = document.forms[0].licenseno.value;

alert("LicNo=" + LicNo);

var strQ1 = "Select Dairy_No from DairyRegister where lic_no =" + LicNo;

alert("strQ1" + strQ1);

var q1 = app1.execute(strQ1,"alis");

alert("Manjeet" + app1.getString(1));

but there is an exception ArrayIndexOutOfBoundsException: -1

How to resolve this?

U have written that this not a good apprach then wat is the alternate.

Thanks.

aarohan_jamwala at 2007-7-10 12:11:38 > top of Java-index,Java Essentials,Java Programming...
# 4

I would suggest writing server-side code (such as a servlet) that queries the database and then presents the result in a form that the javascript can handle.

If the form is XML, then basically you'd be writing AJAX code.

Note that the server-side code shouldn't just take a SQL statement and execute it. You shouldn't expose your database so much; at the very least such a thing might expose data you want kept confidential, but even worse you could open yourself to a cross-site scripting attack. Better, the servlet should be configured with a limited set of safe queries it could perform, and the client would send a token requesting the results of one of the safe queries.

Even if you still use an applet (which doesn't seem necessary) you should still use the servlet. The applet could use it just as easily as the javascript could.

paulcwa at 2007-7-10 12:11:38 > top of Java-index,Java Essentials,Java Programming...
# 5
Dear paulcw,Thanks for the reply.can you send some sample code for my problem (i.e) If the form is XML, then basically you'd be writing AJAX code.I mean AJAX Code.Thank You Very Much.
aarohan_jamwala at 2007-7-10 12:11:38 > top of Java-index,Java Essentials,Java Programming...
# 6
I'm not an AJAX expert. There are lots of forums about it though.Re: the server side stuff, it'll depend on what you have on your server. You might be writing it in Perl, or Python, or shell script, or C, or C++, or Java, or God knows what.
paulcwa at 2007-7-10 12:11:38 > top of Java-index,Java Essentials,Java Programming...