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

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

    I had this exact issue when developing a Spring Boot application in STS, but ultimately deploying the packaged war to WebSphere(v.9). Based on previous answers my situation was unique. ojdbc8.jar was in my WEB-INF/lib folder with Parent Last class loading set, but always it says it failed to find the suitable driver.

    My ultimate issue was that I was using the incorrect DataSource class because I was just following along with online tutorials/examples. Found the hint thanks to David Dai comment on his own question here: Spring JDBC Could not load JDBC driver class [oracle.jdbc.driver.OracleDriver]

    Also later found spring guru example with Oracle specific driver: https://springframework.guru/configuring-spring-boot-for-oracle/

    Example that throws error using org.springframework.jdbc.datasource.DriverManagerDataSource based on generic examples.

    @Config
    @EnableTransactionManagement
    public class appDataConfig {
     \* Other Bean Defs *\
        @Bean
        public DataSource dataSource() {
            // configure and return the necessary JDBC DataSource
            DriverManagerDataSource dataSource = new DriverManagerDataSource("jdbc:oracle:thin:@//HOST:PORT/SID", "user", "password");
            dataSource.setSchema("MY_SCHEMA");
            return dataSource;
        }
    }
    

    And the corrected exapmle using a oracle.jdbc.pool.OracleDataSource:

    @Config
    @EnableTransactionManagement
    public class appDataConfig {
    /* Other Bean Defs */
    @Bean
        public DataSource dataSource() {
            // configure and return the necessary JDBC DataSource
            OracleDataSource datasource = null;
            try {
                datasource = new OracleDataSource();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    
            datasource.setURL("jdbc:oracle:thin:@//HOST:PORT/SID");
            datasource.setUser("user");
            datasource.setPassword("password");
    
            return datasource;
        }
    }
    
    0 讨论(0)
  • 2020-11-21 05:20

    A very silly mistake which could be possible resulting is adding of space at the start of the JDBC URL connection.

    What I mean is:-

    suppose u have bymistake given the jdbc url like

    String jdbcUrl=" jdbc:mysql://localhost:3306/web_customer_tracker?useSSL=false&serverTimeZone=UTC";
    

    (Notice there is a space in the staring of the url, this will make the error)

    the correct way should be:

    String jdbcUrl="jdbc:mysql://localhost:3306/web_customer_tracker?useSSL=false&serverTimeZone=UTC";
    

    (Notice no space in the staring, you may give space at the end of the url but it is safe not to)

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

    I was having the same issue with mysql datasource using spring data that would work outside but gave me this error when deployed on tomcat.

    The error went away when I added the driver jar mysql-connector-java-8.0.16.jar to the jres lib/ext folder

    However I did not want to do this in production for fear of interfering with other applications. Explicity defining the driver class solved this issue for me

        spring.datasource.driver-class-name: com.mysql.cj.jdbc.Driver
    
    0 讨论(0)
  • 2020-11-21 05:27

    I faced the similar issue. My Project in context is Dynamic Web Project(Java 8 + Tomcat 8) and error is for PostgreSQL Driver exception: No suitable driver found

    It got resolved by adding Class.forName("org.postgresql.Driver") before calling getConnection() method

    Here is my Sample Code:

    try {
                Connection conn = null;
                Class.forName("org.postgresql.Driver");
                conn = DriverManager.getConnection("jdbc:postgresql://" + host + ":" + port + "/?preferQueryMode="
                        + sql_auth,sql_user , sql_password);
            } catch (Exception e) {
                System.out.println("Failed to create JDBC db connection " + e.toString() + e.getMessage());
            }
    
    0 讨论(0)
  • 2020-11-21 05:28

    Run java with CLASSPATH environmental variable pointing to driver's JAR file, e.g.

    CLASSPATH='.:drivers/mssql-jdbc-6.2.1.jre8.jar' java ConnectURL
    

    Where drivers/mssql-jdbc-6.2.1.jre8.jar is the path to driver file (e.g. JDBC for for SQL Server).

    The ConnectURL is the sample app from that driver (samples/connections/ConnectURL.java), compiled via javac ConnectURL.java.

    0 讨论(0)
  • 2020-11-21 05:29
    url="jdbc:postgresql//localhost:5432/mmas"
    

    That URL looks wrong, do you need the following?

    url="jdbc:postgresql://localhost:5432/mmas"
    
    0 讨论(0)
提交回复
热议问题