Click to See Complete Forum and Search --> : database connection in solely javascript


nicole1981
09-04-2003, 04:18 AM
hi, can someone help me out on this one?

i have a page residing on server 1 which has to display a dynamic value obtained by querying the database on server 2. but i think that it's not possible to use jsp directly because the database is residing on a different server right?

what i'm told to do is put a javascript file in server 2 and call the js file from server 1. but i'm clueless as to how to access database (sql server) from javascript. can someone provide me with the details, preferably the whole chunk of js codes? and is there any requirement to use such server side javascripting, e.g. server requirements? thanks.

Khalid Ali
09-04-2003, 08:08 AM
JavaScript being srictly the client side scripting lang,is not the tool to connect to the db.
I am not even sure you can use activeXControls to do this(but who knows thats MS,they do the stranges things)
The only time where you can connect to db using JavaScript is I think) iplanet (netscape enterprise server) and javascript will be server side scripting language not the same client side script.

Simply put a very bad idea....

nicole1981
09-04-2003, 09:57 AM
hi, thanks. but in the scenario that i mentioned, is there any way i can retrieve the value from a database in server 2 and use the value in a jsp page in server 1?

Khalid Ali
09-04-2003, 10:20 AM
yes that you can do.
Here is how it can be doen.

1. from jsp page on server 1 send a HTTP request to the server2,

2. on that server using some serverside programming language(JSP or pHP) query the database

3. on the server 1 in jsp page recieve the response and use that value..

you can use URL calss to open a connection and send / recieve data from a url.

nicole1981
09-04-2003, 10:48 AM
hi, thanks for your response but can you elaborate on what you mean by opening a connection with a url class?

i also thought of something similar to what you mentioned. i thought of using a jsp page in server 1 (a.jsp) to call a jsp page in server 2 which query the database and redirect to a.jsp with the required value (retrieved from db) as parameter. is this what you are thinking of? but according to my colleague, i can't use the redirect function across 2 pages in different servers due to some security constraints. what do you think?

Khalid Ali
09-04-2003, 11:37 AM
The security issue is true,but thats limitied to a browser.If you use pure java code

java.net.URL
Then it won't matter.

there are allots of other usefull classes in this package you can use to manipulate a url/resource.

nicole1981
09-04-2003, 12:23 PM
hi, are you saying that i should be using the URL object in my b.jsp (jsp page residing in server where i need to access the database)? i've checked up the api for class URL and i think openConnection() is the method to call to create the url object. does it mean that if i do this:

URL my_url = new URL ("jspPageOnServerOne.jsp?count=10");
my_url.openConnection();

will i be automatically redirect to jspPageOnServerOne.jsp residing in the other server?

thanks again for your help!

Khalid Ali
09-04-2003, 12:32 PM
No what will happen is you will get a url connection object that will have the response of the second server in it.

Below is the example,it should display the result that your other jsp page will return after querying the database.

try{
URL url = new URL("jspPageOnServerOne.jsp?count=10");
URLConnection connection = url.openConnection();
//connection.setRequestProperty("method","POST");
connection.setDoOutput(true);
connection.setRequestProperty( "User-Agent", "Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0; H010818)" );

BufferedReader in = new BufferedReader( new InputStreamReader(connection.getInputStream()));

String line="";
while((line=in.readLine())!=null){
System.out.println(line);
}
}catch(MalformedURLException mfe){
mfe.printStackTrace();
}catch(IOException ioe){
ioe.printStackTrace();
}

once you recieve the response then you can parse the results (line by line ) and make sure what is it you wanted to get from it and then use it in your current jsp page.

nicole1981
09-04-2003, 11:57 PM
hi, i have tried modifying and executing the codes you show me, so i have something like this:

SQL1 = "SELECT count (*) as test FROM PRM_User_Table";
st1 = conn1.createStatement();
rs1 = st1.executeQuery(SQL1);
if(rs1.next()) {
line2=rs1.getString("test");
URL url = new URL("sm_test1.jsp?count="+line2);
line3 = url.getQuery();
URLConnection connection = url.openConnection();
connection.connect();

//connection.setRequestProperty("method","POST");
connection.setDoOutput(true);
connection.setRequestProperty( "User-Agent", "Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0; H010818)" );

BufferedReader in = new BufferedReader( new InputStreamReader(connection.getInputStream()));

line=in.readLine();
while(line!=null){
System.out.println(line);
line=in.readLine();
}
}
else
line2 = "no";


i tried to print "line" and "line3" and both return me empty string. can anyone tell me why? thanks.