The infamous java.sql.SQLException: No suitable driver found

前端 未结 16 1590
广开言路
广开言路 2020-11-21 05:10

I\'m trying to add a database-enabled JSP to an existing Tomcat 5.5 application (GeoServer 2.0.0, if that helps).

The app itself talks to Postgres just fine, so I kn

16条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-21 05:40

    No matter how old this thread becomes, people would continue to face this issue.

    My Case: I have the latest (at the time of posting) OpenJDK and maven setup. I had tried all methods given above, with/out maven and even solutions on sister posts on StackOverflow. I am not using any IDE or anything else, running from bare CLI to demonstrate only the core logic.

    Here's what finally worked.

    • Download the driver from the official site. (for me it was MySQL https://www.mysql.com/products/connector/). Use your flavour here.
    • Unzip the given jar file in the same directory as your java project. You would get a directory structure like this. If you look carefully, this exactly relates to what we try to do using Class.forName(....). The file that we want is the com/mysql/jdbc/Driver.class

    https://i.imgur.com/VgpwatQ.png

    • Compile the java program containing the code.
    javac App.java
    
    • Now load the director as a module by running
    java --module-path com/mysql/jdbc -cp ./ App
    

    This would load the (extracted) package manually, and your java program would find the required Driver class.


    • Note that this was done for the mysql driver, other drivers might require minor changes.
    • If your vendor provides a .deb image, you can get the jar from /usr/share/java/your-vendor-file-here.jar

提交回复
热议问题