aspose file tools
The moose likes HTML, CSS and JavaScript and the fly likes Can i connect a js file to a database? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Reply Bookmark "Can i connect a js file to a database?" Watch "Can i connect a js file to a database?" New topic
Author

Can i connect a js file to a database?

Vishal Hegde
Ranch Hand

Joined: Aug 01, 2009
Posts: 970

Dear Ranchers,

Can I connect, a js file to a database.if yes please let me know or share the link for the same. I tried googling, but it was not displaying


http://www.lifesbizzare.blogspot.com || OCJP:81%
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 35224
    
    7
What do you mean by that? Javascript runs on the client, whereas the DB is on the server. Are you maybe asking : how can Javascript code call server-side code which in turn calls the DB? If so, then a library like jQuery or Prototype may be involved.


Android appsImageJ pluginsJava web charts
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 792
    
    2

Do you mean to connect to database from JavaScript? If that's the case then yes, you can, but it's not recommended to do so (mostly for security issues). It is a bad practice so it would be better to choose a server side language to establish a connection to db.

Also I think there is a separate subforum for these questions (HTML, CSS and JavaScript).


The quieter you are, the more you are able to hear.
Vishal Hegde
Ranch Hand

Joined: Aug 01, 2009
Posts: 970

I created one .js file.

In that I created an Ajax form and that Ajax form is having a checkbox which I want to check it when the value in the (Database)table in Y.

so in .js file how can i write the same script for the same.
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15357
    
    6
Vishal Hegde wrote:I created one .js file.

In that I created an Ajax form and that Ajax form is having a checkbox which I want to check it when the value in the (Database)table in Y.

so in .js file how can i write the same script for the same.


JavaScript can not connect directly to the server so you will need to make an Ajax call back to a serverside page that can check the database for you. Other option is to have the page spit out the information you need in a script block when the page is rendering and the script uses that information.

Eric
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 56166
    
  13

Or if you are using JSP on the server, simply have the JSP set the attributes as appropriate before the page even reaches the browser.

As pointed out, JavaScript in the browser cannot directly access the DB.


[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Can i connect a js file to a database?
 
Similar Threads
Alert message translations - design question
jquery in Eclipse
contextpath in javscript
How to prevent from directly loading javascript file from browser URL?
formElement.js in webui.jar in jsf