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

前端 未结 16 1558
广开言路
广开言路 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
    0 讨论(0)
  • 2020-11-21 05:40

    I encountered this issue by putting a XML file into the src/main/resources wrongly, I deleted it and then all back to normal.

    0 讨论(0)
  • 2020-11-21 05:43

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

    This exception can have basically two causes:

    1. JDBC driver is not loaded

    You need to ensure that the JDBC driver is placed in server's own /lib folder.

    Or, when you're actually not using a server-managed connection pool data source, but are manually fiddling around with DriverManager#getConnection() in WAR, then you need to place the JDBC driver in WAR's /WEB-INF/lib and perform ..

    Class.forName("com.example.jdbc.Driver");
    

    .. in your code before the first DriverManager#getConnection() call whereby you make sure that you do not swallow/ignore any ClassNotFoundException which can be thrown by it and continue the code flow as if nothing exceptional happened. See also Where do I have to place the JDBC driver for Tomcat's connection pool?

    2. Or, JDBC URL is in wrong syntax

    You need to ensure that the JDBC URL is conform the JDBC driver documentation and keep in mind that it's usually case sensitive. When the JDBC URL does not return true for Driver#acceptsURL() for any of the loaded drivers, then you will also get exactly this exception.

    In case of PostgreSQL it is documented here.

    With JDBC, a database is represented by a URL (Uniform Resource Locator). With PostgreSQL™, this takes one of the following forms:

    • jdbc:postgresql:database
    • jdbc:postgresql://host/database
    • jdbc:postgresql://host:port/database

    In case of MySQL it is documented here.

    The general format for a JDBC URL for connecting to a MySQL server is as follows, with items in square brackets ([ ]) being optional:

    jdbc:mysql://[host1][:port1][,[host2][:port2]]...[/[database]] » [?propertyName1=propertyValue1[&propertyName2=propertyValue2]...]

    In case of Oracle it is documented here.

    There are 2 URL syntax, old syntax which will only work with SID and the new one with Oracle service name.

    Old syntax jdbc:oracle:thin:@[HOST][:PORT]:SID

    New syntax jdbc:oracle:thin:@//[HOST][:PORT]/SERVICE


    See also:

    • Where do I have to place the JDBC driver for Tomcat's connection pool?
    • How to install JDBC driver in Eclipse web project without facing java.lang.ClassNotFoundexception
    • How should I connect to JDBC database / datasource in a servlet based application?
    • What is the difference between "Class.forName()" and "Class.forName().newInstance()"?
    • Connect Java to a MySQL database
    0 讨论(0)
  • 2020-11-21 05:45

    It might be worth noting that this can also occur when Windows blocks downloads that it considers to be unsafe. This can be addressed by right-clicking the jar file (such as ojdbc7.jar), and checking the 'Unblock' box at the bottom.

    Windows JAR File Properties Dialog:
    Windows JAR File Properties Dialog

    0 讨论(0)
提交回复
热议问题