I’m using Eclipse/Tomcat10/MySql and have added both Java jdbc libraries to my classpath. Major clue: This works as long as I refactor the servlet to a POJO, loading the driver in main(). When I refactor back to a servlet, suddenly the classloader can’t find the driver.
As I am using JDK21 and such, there is no need to test the loading with Class.forName() but I’ve tried that. Currently scaled back to just using
Connection cn = DriverManager.getConnection(url,uname,pwd); where it fails with a SQLException and “no suitable driver found …”
So it works as a POJO and fails as a servlet. Here is what I tried:
- New Thread inside doGet() (which is called by a jsp)
- Thread.currentThread().getContextClassLoader().loadClass(“com.mysql.cj.jdbc.Driver”);
- Pointing to the older “com.mysql.jdbc.Driver”
- After noticing stale debug output to console I started running, killing, and running again to make sure I had the correct/non-cached version of the servlet.
- Put the code in doPost() and calling that method with the jsp. Joke, but I had to try.
- Of course tried Class.forName(“com.mysql.cj.jdbc.Driver”);
- Tried Project > Clean
- Tried Restarting Eclipse
- Tried having both jdbc packages together, then tried each one separately in case there was a classpath clash.
Number 10. is left open in case any of this rings a bell with you. I’m just going to guess newbie guesses that servlet classloading is somewhat of an “art” and not a science, haha.