Good day! Here's the situation. We are trying to connect our mySQL database using javascript. We already have the java codes to access the database, but the thing is, they are java files and I cannot find ways how to transform them to javascripts. How can these different classes (e.g. DatabaseClass.java, DBConnectionFactory.java, DBConnectionFactoryImpl.java) work together in an HTML file? Can someone provide a link which could help us with this?
P.S. We're just beginners. I hope you guys can understand what we are trying to do. Cheers!
-
See the answer to this question.Buggabill– Buggabill2013年11月04日 13:23:02 +00:00Commented Nov 4, 2013 at 13:23
-
If you are thinking of doing front-end database connections i would STRONGLY suggest another approach. That is a very big vulnerability, due to the fact that any client have full control of the client-side code.Jite– Jite2013年11月04日 13:28:24 +00:00Commented Nov 4, 2013 at 13:28
-
I have read some of the disadvantages of using javascript to connect to the database. However, we are required to use javascript on this project.user2836881– user28368812013年11月04日 13:37:09 +00:00Commented Nov 4, 2013 at 13:37
-
You should ask for clarification on the project rules in that case. Maybe you are intended to use server-side js, Node.js or something? I cant believe that any teacher or senior (not sure if school or work) would suggest to do client-side database connections.Jite– Jite2013年11月04日 13:40:19 +00:00Commented Nov 4, 2013 at 13:40
-
Reading the other comments and posts, I would believe that your professor have created a API that you are supposed to use via javascript. Could this be the case? Then its probably AJAX you are after.Jite– Jite2013年11月04日 13:42:28 +00:00Commented Nov 4, 2013 at 13:42
4 Answers 4
You're mistaking that Java and Javascript are somehow related. They're not, and you can't just magically use Java classes in Javascript.
You need to have Java access the database, and communicate with the Java backend from Javascript.
4 Comments
You need to make a server call to get data from the database. It is bad practice to call the database directly from the client side (you don't want to expose a database login to the client).
If you know what to look for when the page is called you can make the page a servlet, JSP, CGI-script or any other technology that facilitates dynamic web pages.
If you know (or want to learn) java I would recommend a servlet (with java code) that forwards to a jsp (for the presentation). Note that you need an application server that supports servlets and jsp.
If you need to make the database call on the fly, after the page has loaded, you can use Ajax or AJAJ to call a servlet (or other server side facility).
1 Comment
my two cents: if you need to work with Java and a DB, don't go for HTML. HTML is static, while it seems you want a dynamic application. then the best option (for beginners) would be to learn basic JSP and Servlets.
it is possible to access a database using JavaScript (though I wouldn't recommend it), here and here you can find topics on that subject.
13 Comments
I think you'd better learn basic JSP/servlet , and then you can use then through a web server like tomcat ...