cancel
Showing results for 
Search instead for 
Did you mean: 

MySql db connection through JavaScript

msvoren
Champ in-the-making
Champ in-the-making
Hello,
Can i connect to db through javascript in Alfresco?

I need to fetch some data from db, just read from it.. simple "fetch" of 1 field..

If not possible, what are my second options?

Thanks everybody
6 REPLIES 6

pmonks
Star Contributor
Star Contributor
From 2.1.3 on you can access Java APIs from Javascript - see http://wiki.alfresco.com/wiki/JavaScript_API#Native_Java_API_Access.  This would give you access to the JDBC API (or SpringJDBC or Hibernate or whatever) from Javascript, albeit it won't necessarily be easy to do in a production-grade fashion (eg. exception handling can be difficult to do properly in Javascript).

Cheers,
Peter

msvoren
Champ in-the-making
Champ in-the-making
found this article:

http://peter.michaux.ca/article/3019
                Appendix 2: talking with the database from the Rhino shell

I make java file, put it in source, org.alfresco.mysqlconnect; I compile the code..

This should work, but I don't know how to call java function with javascript in alfresco..

Anybody?
thanks

pmonks
Star Contributor
Star Contributor
http://wiki.alfresco.com/wiki/JavaScript_API#Native_Java_API_Access describes how (in 2.1.3 and beyond) you can access Java APIs from Javascript running within Alfresco.

Cheers,
Peter

stebans
Champ in-the-making
Champ in-the-making
found this article:

http://peter.michaux.ca/article/3019
                Appendix 2: talking with the database from the Rhino shell

I make java file, put it in source, org.alfresco.mysqlconnect; I compile the code..

This should work, but I don't know how to call java function with javascript in alfresco..

Anybody?
thanks

I've been making some trials with javascript script calling Java. I put jars in lib folder along with other libs in classpath.
You're done, classes are available!
You can use them directly using their full name :
    java.io.File
or
var File = java.io.File
function fun(){
   var myfile =  new File("myFile");…
}

importPackage doesn't work in my configuration (Alfresco-2.1CE) (see: http://forums.alfresco.com/viewtopic.php?t=9711). Upgrading Rhino to 1.7R1 seems to be OK as far as I've tried it.


On the other hand, I don't know how to deal with Java Exceptions and if there are best practices for importing Java classes. I don't know neither how to use the classpath for reading a file from say, extension folder. Answers welcome!

HTH,
Regards,
Stéphane

msvoren
Champ in-the-making
Champ in-the-making
Thank you for your reply!
I already tried to put this java function in web client's lib file..

Maybe my synthax of calling api from js was not correct.. I will try with your example, will let you know.

Thanks again

stebans
Champ in-the-making
Champ in-the-making
Hi,

updating Rhino with Rhino-1.7R1 allowed me to import java packages in 2.1CE, although importPackage/importClass were disabled. I then realized that it was a big security problem… There has been discussion about allowing javascript use java classes if the script is in the classpath, as noted in the wiki
As of Alfresco 2.1.3, Javascript scripts stored in the classpath can also leverage the Rhino Javascript interpreter's native Java integration facilities (see the Rhino documentation for more details). For security reasons these mechanisms are completely disabled for Javascript scripts that are stored in the repository.
So this fonctionality should be available in Alf-Labs3.0, right? What's wrong with this script 'test.js' calling java, with, say, new java.io.File("…") imported in a script?
<import resource="classpath:alfresco/extension/test.js">‍
which complains that "ReferenceError: "java" is not defined"?
Could anyone you show an example, or add one in the cookbook?

Thanks a lot,
Best regards,
stebans