i got the error above and below is my code what am i doin wrong? the error reference to the datasource. what is the problem?
public class DBConnector {
private Connection dbConn = null;
private FileLogger fileLogger = new FileLogger("./logs/logfile.txt");
private ConfigurationReader conf = new ConfigurationReader("./config.sys");
private Logger logger = fileLogger.getLogger();
/**
* Attempts to establish a connection with a test MySQL database.
*/
public DBConnector() {
MysqlDataSource dataSource = new MysqlDataSource();
dataSource.setUser(conf.getConfigString("username"));
dataSource.setPassword(conf.getConfigString("password"));
dataSource.setServerName(conf.getConfigString("dbhost"));
dataSource.setDatabaseName(conf.getConfigString("dbname"));
dataSource.setPort(conf.getConfigInt("dbport"));
try {
dbConn = dataSource.getConnection();
logger.info("Succesful connection with database!");
} catch (Exception ex) {
logger.info("Error: DBConnector(): problem creating database connection");
}
logger.info("Database connection = " + dbConn);
}
I got the same error (on Java 9 or above). For me the problem was due to a missing requires java.naming;
directive in the module-info.java
of my module (the module, that accesses Referenceable). Adding this directive fixed it:
module MyModule {
requires java.naming;
...
}
I hope this helps.
As explained by Sander Mak here this usually happens when your code depends on something that does not have a module descriptor and this dependency's dependency is unresolved (e.g. you're using Hibernate which has no module descriptor but it requires java.naming).
In this case, rather than polluting your own module descriptor with the transitive dependency, Sander suggests to add the transitive dependency to your module graph using
--add-modules java.naming
This way when Hibernate in the future gets its own module descriptor you can just remove the above line and have only your direct dependencies in your module-info.java.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With