0

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](http://stackoverflow.com/questions/3020751/can-javascript-connect-with-mysql). – Buggabill Nov 04 '13 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 Nov 04 '13 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 Nov 04 '13 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 Nov 04 '13 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 Nov 04 '13 at 13:42
  • You can't access databases from JavaScript and you shouldn't try. – Boann Nov 04 '13 at 22:56

4 Answers4

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.

Kayaman
  • 72,141
  • 5
  • 83
  • 121
  • So the codes that we have done using an IDE is totally different from what we will be using to write an HTML file? – user2836881 Nov 04 '13 at 13:25
  • That's a broad question, which is also technology dependent, but basically yes. Which technologies are you using? Servlets on Tomcat? JSP? Pure Javascript? – Kayaman Nov 04 '13 at 13:28
  • I believe that is only pure javascript that we're dealing with. Our problem is that our professor left us with working java DB connector files, and he expects us to be able to access the database with our HTML projects using javascript. – user2836881 Nov 04 '13 at 13:34
  • 1
    He can't be expecting that, since it's not possible (except in a very limited sense). You'll need to clarify the requirements from your teacher. – Kayaman Nov 04 '13 at 13:37
2

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).

Klas Lindbäck
  • 33,105
  • 5
  • 57
  • 82
  • 1
    Exactly that. (client side) Javascript code is as visible as you can make it and if it wants to connect to a database, you need to store the connection details SOMEWHERE, which is going to be where the javascript code can reach it, and thus the browser can reach it because it is the browser that will be executing said Javascript, and thus anyone using the browser can also reach it since whatever is visible to the browser is visible to its user no matter how much trickery you throw at it to hide implementation details. Its just a Really Bad Idea (TM). – Gimby Nov 04 '13 at 13:51
0

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.

Community
  • 1
  • 1
Stultuske
  • 9,296
  • 1
  • 25
  • 37
  • I'll eat my shorts if the professor wants them to use an IE specific Windows solution. But seems you got the "correct answer". – Kayaman Nov 04 '13 at 13:38
  • Those snippets gave me an idea. Thanks! One more thing, how about those java imports which have to be declared so classes would work properly? Aren't they important when it comes to javascript? – user2836881 Nov 04 '13 at 13:39
  • 1
    You aren't actually considering calling the database directly from javascript? Seriously? DON'T! It is wrong in so many ways. – Klas Lindbäck Nov 04 '13 at 13:49
  • I believe it is wrong too. However, it is what he is requiring from us. – user2836881 Nov 04 '13 at 13:53
  • 3
    Just don't submit it as a work sample when you are looking for a job. – Klas Lindbäck Nov 04 '13 at 13:55
  • You really should ask him if this is really what he wants. Cause it seems a lot more likely that you misunderstood than that he would teach you very very very bad practices. – Jite Nov 04 '13 at 13:56
  • @Jite Well, we've been practicing java all throughout course and I guess it is quite reasonable for those who aren't done with all java courses to use javascript on this matter. By the way, we are expected to use javascript since he has already provided the java code for the connection. – user2836881 Nov 04 '13 at 14:04
  • Did he provide an API written in java or did he provide a bunch of java code that you are supposed to convert to javascript for some odd reason? – Jite Nov 04 '13 at 14:05
  • @KlasLindbäck - Noted! Cheers! – user2836881 Nov 04 '13 at 14:06
  • @Jite he provided a bunch of java code. I suppose that serves as a guide for us. – user2836881 Nov 04 '13 at 14:07
  • 1
    Does it run? Cause I cant believe that any teacher would supply you with server-side java code to look at and convert to client-side javascript, including DB connections and all. That would be like giving you a recipe on stew as an example on how to cut down a tree. – Jite Nov 04 '13 at 14:09
  • Yes, it runs. It can actually access the database, but that's where we stopped. – user2836881 Nov 04 '13 at 14:10
  • Then it is most likely a API that you are supposed to send requests to from the javascript code. That is how it is done. – Jite Nov 04 '13 at 14:14
0

I think you'd better learn basic JSP/servlet , and then you can use then through a web server like tomcat ...

Crowley
  • 27
  • 1
  • 1
  • 8