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

前端 未结 16 1559
广开言路
广开言路 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:33

    In my case I was working on a Java project with Maven and encountered this error. In your pom.xml file make sure you have this dependencies

    <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>4.11</version>
          <scope>test</scope>
        </dependency>
        <dependency>
          <groupId>mysql</groupId>
          <artifactId>mysql-connector-java</artifactId>
          <version>8.0.11</version>
        </dependency>
      </dependencies>
    

    and where you create connection have something like this

    public Connection createConnection() {
            try {
                String url = "jdbc:mysql://localhost:3306/yourDatabaseName";
                String username = "root"; //your my sql username here
                String password = "1234"; //your mysql password here
    
                Class.forName("com.mysql.cj.jdbc.Driver");
                return DriverManager.getConnection(url, username, password);
            } catch (SQLException | ClassNotFoundException e) {
                e.printStackTrace();
            }
    
            return null;
        }
    
    0 讨论(0)
  • 2020-11-21 05:34

    You will get this same error if there is not a Resource definition provided somewhere for your app -- most likely either in the central context.xml, or individual context file in conf/Catalina/localhost. And if using individual context files, beware that Tomcat freely deletes them anytime you remove/undeploy the corresponding .war file.

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

    I've forgot to add the PostgreSQL JDBC Driver into my project (Mvnrepository).

    Gradle:

    // http://mvnrepository.com/artifact/postgresql/postgresql
    compile group: 'postgresql', name: 'postgresql', version: '9.0-801.jdbc4'
    

    Maven:

    <dependency>
        <groupId>postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>9.0-801.jdbc4</version>
    </dependency>
    

    You can also download the JAR and import to your project manually.

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

    As well as adding the MySQL JDBC connector ensure the context.xml (if not unpacked in the Tomcat webapps folder) with your DB connection definitions are included within Tomcats conf directory.

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

    I found the followig tip helpful, to eliminate this issue in Tomcat -

    be sure to load the driver first doing a Class.forName(" org.postgresql.Driver"); in your code.

    This is from the post - https://www.postgresql.org/message-id/e13c14ec050510103846db6b0e@mail.gmail.com

    The jdbc code worked fine as a standalone program but, in TOMCAT it gave the error -'No suitable driver found'

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

    I was using jruby, in my case I created under config/initializers

    postgres_driver.rb

    $CLASSPATH << '~/.rbenv/versions/jruby-1.7.17/lib/ruby/gems/shared/gems/jdbc-postgres-9.4.1200/lib/postgresql-9.4-1200.jdbc4.jar'
    

    or wherever your driver is, and that's it !

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