0

So, I have a node.js project that I am working on for a class. One of the features our group is trying to build, requires calling a Java class method that connects to a MySQL database, does some computation and returns the result to the node server. Here is what we have so far:

Using the node-java library, we are adding the required jars to classpath:

var java = require('java');
var path = require('path');
java.classpath.push(path.join(__dirname, "../java/mysql-connector-java.5.1.38-bin.jar"));
java.classpath.push(path.join(__dirname, "../java/BayesianCurve.jar"));
java.classpath.push(path.join(__dirname, "../java/commons-math3-3.6.jar"));

Then, the class is instantiated and a method is called:

var test = java.newInstanceSync('BayesianCurve');
test.getPrediction(param, function(err, data){
    if(err)
    {
        console.log(err);
        return;
    }
    else
        res.send(JSON.stringify(data));
});

So far, so good. The Java class method then has to create a connection to a MySQL database and this is where the problems start:

Class.forName("com.mysql.jdbc.Driver");
//System.setProperty("jdbc.drivers","com.mysql.jdbc.Driver");
//DriverManager.registerDriver(new com.mysql.jdbc.Driver());
System.out.println("Here");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/schema","root","root");

The java code, standalone, is able to connect to the database and execute queries. However, when used through the node-java module, it throws:

ClassNotFoundException: com.mysql.jdbc.Driver

One reason we thought about was that this library is linked dynamically. Somehow, the java environment in node.js is not able to figure this out. However, we are not really sure how to resolve this. Any help in this regard is appreciated.

  • The JDBC driver needs to be on the classpath of the application. Find out how you need to configure the classpath when accessing Java from Node.js. – Mark Rotteveel Apr 24 '16 at 08:36
  • The JDBC driver is present in the `mysql-connector-java.5.1.38-bin.jar`, which I am adding to the node-java classpath using `java.classpath.push()`. This method works for the other jars I have added, but apparently not for this one. – TheDarkKnight Apr 24 '16 at 17:07

0 Answers0