The Cannot load JDBC driver class 'org.postgresql.Driver' error occurs when working with Spring.

Database Settings:

 @Bean public DataSource dataSource() { BasicDataSource ds = new BasicDataSource(); ds.setUrl(env.getRequiredProperty("db.url")); ds.setDriverClassName(env.getRequiredProperty("db.driver")); ds.setUsername(env.getRequiredProperty("db.username")); ds.setPassword(env.getRequiredProperty("db.password")); ds.setInitialSize(Integer.valueOf(env.getRequiredProperty("db.initialSize"))); ds.setMinIdle(Integer.valueOf(env.getRequiredProperty("db.minIdle"))); ds.setMaxIdle(Integer.valueOf(env.getRequiredProperty("db.maxIdle"))); ds.setTimeBetweenEvictionRunsMillis(Long.valueOf(env.getRequiredProperty("db.timeBetweenEvictionRunsMillis"))); ds.setMinEvictableIdleTimeMillis(Long.valueOf(env.getRequiredProperty("db.minEvictableIdleTimeMillis"))); ds.setTestOnBorrow(Boolean.valueOf(env.getRequiredProperty("db.testOnBorrow"))); ds.setValidationQuery(env.getRequiredProperty("db.validationQuery")); return ds; } 

The properties indicate db.driver = org.postgresql.Driver . What could be wrong with the driver?

  • one
    JAR with the driver lies in CLASSPATH? - Nofate

1 answer 1

In order for java to create an object of class org.postgresql.Driver , the driver jar file (for example, postgresql-9.4.1209.jar ) in which this class is declared must be in the CLASSPATH .

Most likely, in your IDE (you are not writing a spring-application in a notebook?) You can connect libraries to the project, specifying the paths to the jar-files. The postgresql driver can be downloaded from here:

https://jdbc.postgresql.org/download.html

Also, a web server (for example, Apache Tomcat) usually has a lib folder where you can put a driver jar file for use by applications.