[英]Cannot connect my nodejs to mysql
My code is as below 我的代码如下
var JDBC = require('jdbc');
var jinst = require('jdbc/lib/jinst');
var Pool = require('jdbc/lib/pool');
// isJvmCreated will be true after the first java call. When this happens, the
// options and classpath cannot be adjusted.
if (!jinst.isJvmCreated()) {
// Add all java options required by your project here. You get one chance to
// setup the options before the first java call.
jinst.addOption("-Xrs");
// Add all jar files required by your project here. You get one chance to
// setup the classpath before the first java call.
jinst.setupClasspath(['./jars/mysql-connector-java-5.1.38-bin.jar']);
}
var mySql = new JDBC({
url: 'jdbc:mysql://localhost:3306/nodejs',
drivername: 'com.mysql.jdbc.Driver',
minpoolsize: 5,
maxpoolsize: 10,
user: 'root',
password: 'root'
});
mySql.initialize(function(err) {
if (err) {
console.log(err);
}
});
I have the jar in the folder and mysql running in my local. 我在文件夹中有jar,在本地运行mysql。 But i am getting the following error
但是我收到以下错误
java.lang.NoClassDefFoundError: com/mysql/jdbc/Driver
Caused by: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
] cause: nodeJava_java_lang_NoClassDefFoundError {} }
{ [Error: Error running static method
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/nodejs
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
] cause: nodeJava_java_sql_SQLException {} }
Please help. 请帮忙。 Is there any initialisation commands neede in nodejs?
nodejs中是否需要初始化命令?
I tried your code exactly and it works, I think your path is setupClasspath()
is not good. 我完全尝试了您的代码,它可以正常工作,我认为您的路径为
setupClasspath()
不好。 Try using an absolute path, a relative path works only if the working directory is set like you expect: 尝试使用绝对路径,只有在按预期方式设置工作目录时,相对路径才有效:
if (!jinst.isJvmCreated()) {
jinst.addOption("-Xrs");
jinst.setupClasspath([__dirname + '/jars/mysql-connector-java-5.1.38-bin.jar']);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.