AnsweredAssumed Answered

Custom Assertion: ClassNotFoundException - JDBC Driver

Question asked by LLenz on Aug 15, 2018
Latest reply on Sep 5, 2018 by LLenz

Hi,

 

I'm developing a Custom Assertion for the CA API Gateway that uses the jdbc Driver to connect to a database. The first version of the Custom Assertion did not has a custom UI. I just used the default one, that is created by the Gateway.

 

The current version should have its' own custom UI. I want to collect and check the database parameters in this UI. When I try to establish the connection i get the following Exception:

 

ClassNotFoundException:

Message: com.mysql.jdbc.Driver

Stacktrace:

com.l7tech.console.util.CustomAssertionClassLoader.findClass(Unknown Source)

java.lang.ClassLoader.loadClass(ClassLoader.java:424)

java.lang.ClassLoader.loadClass(ClassLoader.java:357)

java.lang.Class.forName()(Native Method)

java.lang.Class.forName(Class.java:264)

com.l7tech.custom.xmlassertion.Database.<init>(Unknown Source)

com.l7tech.custom.xmlassertion.Database.getInstance(Unknown Source)

com.l7tech.custom.xmlassertion.MainWindow$2.actionPerformed(Unknown Source)

...

...

...

 

Obviously the jdbc Driver cannot be loaded. I tried to add the mysql-jdbc-connector to the ANT Buildfile of the project without success. Here is my code:

MainWindow.java

...

btn_test_connection.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {

String ip = jT_db_ip.getText();
String port = jT_db_port.getText();
String user = jT_db_user.getText();
char[] password = jP_password.getPassword();
String name = jT_db_name.getText();

if (
ip.length() == 0 ||
port.length() == 0 ||
user.length() == 0 ||
password.length == 0 ||
name.length() == 0
) {
JOptionPane.showMessageDialog(MainWindow.this, "All fields must be filled!", "Warning", HEIGHT);
} else {
try {
Database.getInstance(ip, port, name, user, new String(password));
jL_status.setText("Connection successfully established.");
jL_status.setForeground(Color.GREEN);
jB_ok.setEnabled(true);
}
catch (Exception ex) {
jL_status.setText("Connection could not be established.");
jL_status.setForeground(Color.RED);
jB_ok.setEnabled(false);
String stack = "";
for (int i = 0; i < ex.getStackTrace().length; i++) {
stack += ex.getStackTrace()[i] + "\n";
}
JOptionPane.showMessageDialog(MainWindow.this, "Error Message:\n" + ex.getMessage() + "\nStacktrace:\n" + stack, ex.getClass().toString(), HEIGHT);
}
}

}
});

...

Database.java

public class Database {

private static Connection connection = null;

private Database(String databaseIP, String databasePort, String databaseName, String databaseUser, String databasePassword) throws SQLException, ClassNotFoundException {
XML_AssertionServiceInvocation.logger.config("CUSTOM ASSERTION [XML_Assertion]: Loading JDBC driver");
Class.forName("com.mysql.jdbc.Driver"); // Datenbanktreiber fuer JDBC Schnittstellen laden.
Properties connectionProperties = new Properties();
connectionProperties.put("autoReconnect", "true");

// Verbindung zur JDBC-Datenbank herstellen.
connection = DriverManager.getConnection("jdbc:mysql://" + databaseIP + ":" + databasePort + "/" + databaseName + "?" + "user="
+ databaseUser + "&" + "password=" + databasePassword, connectionProperties);
}

public static Connection getInstance(String databaseIP, String databasePort, String databaseName, String databaseUser, String databasePassword) throws SQLException, ClassNotFoundException {
if (connection == null) {
new Database(databaseIP, databasePort, databaseName, databaseUser, databasePassword);
}
return connection;
}
}

}

 

As i mentioned, before the UI was added everything worked just fine.

 

I hope you guys have any ideas.

 

Best Regards

Lucas

Outcomes