ClassNotFoundException: com.mysql.jdbc.Driver [message #1855228] |
Mon, 03 October 2022 08:42 |
Bart Devos Messages: 2 Registered: October 2022 |
Junior Member |
|
|
Hi list,
I try to make a EASE javascript which needs information from a mysql database and based on that, appends text to the selected java editor. I made a eclipse plugin project as described in codeandme.blogspot.com/2014/10/writing-modules-for-ease.html. I added the logic (simplyfied for the example) as follows:
package be.zokola.main;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import org.eclipse.ease.modules.WrapToScript;
public class MysqlTableQueries {
private String driver = "com.mysql.jdbc.Driver";
private String dburl = "jdbc:mysql://localhost:3306/";
private String dbName = "zokoladblabeltest";
private String credname = "<myDBUsername>";
private String cred = "<myDBPassword>";
private String tableName = null;
private Connection conn;
public MysqlTableQueries() {
}
@WrapToScript
public String test() {
return "Helllooowwww....";
}
/**
* Make a connection to the database. If dbName is ommitted, zokoladblabelstest is used
* @param dbName
* @throws ClassNotFoundException
* @throws SQLException
*/
@WrapToScript
public void connect2db() throws ClassNotFoundException, SQLException {
if(dbName != null) {
this.dbName = dbName;
}
Class.forName(driver);
this.conn = DriverManager.getConnection(dburl+dbName, credname, cred);
}
public Boolean doesTableExist(String tableName) throws SQLException {
Integer result = 0;
this.tableName = tableName;
String q = "SELECT COUNT(*) "
+ "FROM information_schema.tables "
+ "WHERE table_schema = \"" + dbName + "\" "
+ "AND table_name = \"" + tableName + "\";";
Statement st = null;
ResultSet rs = null;
try {
st = conn.createStatement();
rs = st.executeQuery(q);
if(rs.next()) {
result = rs.getInt(1);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
st.close();
}
if(result == 1) {
return true;
} else {
return false;
}
}
Now my EASE script:
/**
* Compares a given entity with a table in the database
* name: DiffTableEntity
* popup: enableFor(org.eclipse.jface.text.ITextSelection)
* image: ../icns/MySQL-Logo2.png
*/
loadModule('/System/UI');
loadModule("/System/JVM", false);
module = createInstance("workspace://EASEscript/src/be/zokola/main/MysqlTableQueries.java");
wrap(module);
var question1 = "Databasename:";
var defaultvalue1 = "zokoladblabeltest";
var title1 = "Enter the databasename to query";
var question2 = "Tablename:";
var defaultvalue2 = "";
var title2 = "Enter the table to query";
var dbname = showInputDialog(question1, defaultvalue1, title1);
if(dbname != null & dbname != ""){
var table = showInputDialog(question2, defaultvalue2, title2);
if(table != null && table != ""){
print("============>>>>>>>>>>" + test());
connect2db();
print("============>>>>>>>>>>" + doesTableExist(table));
print(dbname + " - " + table);
closeConnection();
}
}
What does work:
1) Executing scriptmethod "test" (see print("============>>>>>>>>>>" + test());
2) Calling "doesTableExist(String table)" from a test class in java. So, the mysql driver is loaded correctly in that case.
What does NOT work:
print("============>>>>>>>>>>" + doesTableExist(table)); from the script. I get following error stack trace:
============>>>>>>>>>>Helllooowwww....
org.eclipse.ease.ScriptExecutionException: JavaError: com.mysql.jdbc.Driver
at DiffTableEntity.js,workspace://EASEproject/livescripts/DiffTableEntity.js
Java Stacktrace:
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.base/java.net.URLClassLoader.findClass(URLClassLoader.java:476)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:589)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
at java.base/java.lang.Class.forName0(Native Method)
at java.base/java.lang.Class.forName(Class.java:315)
at be.zokola.main.MysqlTableQueries.connect2db(MysqlTableQueries.java:48)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.mozilla.javascript.MemberBox.invoke(MemberBox.java:126)
at org.mozilla.javascript.NativeJavaMethod.call(NativeJavaMethod.java:225)
at org.mozilla.javascript.gen.c_17._c_connect2db_4(Unknown Source)
at org.mozilla.javascript.gen.c_17.call(Unknown Source)
at org.mozilla.javascript.optimizer.OptRuntime.callName0(OptRuntime.java:74)
at org.mozilla.javascript.gen.workspace___EASEproject_livescripts_DiffTableEntity_js_13._c_script_0(Unknown Source)
at org.mozilla.javascript.gen.workspace___EASEproject_livescripts_DiffTableEntity_js_13.call(Unknown Source)
at org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:405)
at org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3508)
at org.mozilla.javascript.gen.workspace___EASEproject_livescripts_DiffTableEntity_js_13.call(Unknown Source)
at org.mozilla.javascript.gen.workspace___EASEproject_livescripts_DiffTableEntity_js_13.exec(Unknown Source)
at org.mozilla.javascript.Context.evaluateReader(Context.java:1293)
at org.eclipse.ease.lang.javascript.rhino.RhinoScriptEngine.internalExecute(RhinoScriptEngine.java:223)
at org.eclipse.ease.lang.javascript.rhino.RhinoScriptEngine.execute(RhinoScriptEngine.java:202)
at org.eclipse.ease.AbstractScriptEngine.inject(AbstractScriptEngine.java:190)
at org.eclipse.ease.AbstractScriptEngine.run(AbstractScriptEngine.java:243)
at org.eclipse.core.internal.jobs.Worker.run(Worker.java:63)
Why is the mysql driver correctly loaded when executing from java but not when called from the EASE script. Please, any advice would be much appreciated since i'm fairly new to EASE and building modules.
BTW: This is an excellent addition to the vast eclipse eco-system!
Kind regards,
Bart Devos
Belgium.
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.06024 seconds