Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can i connect a js file to a database?

 
Vishal Hegde
Ranch Hand
Posts: 1077
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Vishal Hegde
Ranch Hand
Posts: 1077
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65116
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic