1
votes

I am aware this issue comes up when no proper jdbc driver jar is configured in the build path, I have tried adding a few jdbc jars for postgres, yet I face the issue. Please find the below jars I tried.

Using

  • postgres : 1.16.1
  • Eclipse Version: Indigo Service Release 2
  • Java version : 8

Jars I tried

  1. postgresql-9.4.1208.jre6
  2. postgresql-connector-jdbc4.jar
  3. postgresql-jdbc.jar
  4. postgresql-9.3-1103.jdbc3
  5. postgresql-9.2-1003-jdbc4-sources.jar
  6. pg73jdbc3
  7. jdbc7.1-1.1

Reading other posts on stack overflow, I even tried setting the system environmental variables as below..

  • User Variables - Admin - classpath

C:\Program Files\PostgreSQL\9.2\lib

  • System Variable - path

C:\Program Files\PostgreSQL\9.2\bin

Not sure if this is required

Database details setup in my .properties file

jdbc.driverClassName=org.postgresql.Driver
jdbc.url=postgresql://localhost:5432/postgres
jdbc.username=admin
jdbc.password=admin

Using spring framework based application

Logs :

org.springframework.transaction.CannotCreateTransactionException: Could not open JDBC Connection for transaction; nested exception is java.sql.SQLException: No suitable driver found for postgresql://localhost:5432/postgres
    at org.springframework.jdbc.datasource.DataSourceTransactionManager.doBegin(DataSourceTransactionManager.java:240)
    at org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:371)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary(TransactionAspectSupport.java:335)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:105)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
    at $Proxy8.getCollegeDetails(Unknown Source)
    at com.cts.bo.HESBO.registerCourse(HESBO.java:42)
    at com.cts.facade.HESFacade.registerCourse(HESFacade.java:34)
    at com.cts.manager.HESManager.registerCourse(HESManager.java:34)
    at com.cts.presentation.Tester.registerCourse(Tester.java:66)
    at com.cts.presentation.Tester.main(Tester.java:159)
Caused by: java.sql.SQLException: No suitable driver found for postgresql://localhost:5432/postgres
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at org.springframework.jdbc.datasource.DriverManagerDataSource.getConnectionFromDriverManager(DriverManagerDataSource.java:173)
    at org.springframework.jdbc.datasource.DriverManagerDataSource.getConnectionFromDriver(DriverManagerDataSource.java:164)
    at org.springframework.jdbc.datasource.AbstractDriverBasedDataSource.getConnectionFromDriver(AbstractDriverBasedDataSource.java:149)
    at org.springframework.jdbc.datasource.AbstractDriverBasedDataSource.getConnection(AbstractDriverBasedDataSource.java:119)
    at org.springframework.jdbc.datasource.DataSourceTransactionManager.doBegin(DataSourceTransactionManager.java:202)
    ... 11 more

Please guide as for what needs to be done.Thanks

1
Your database JDBC URL is wrong, it should start with "jdbc:postgresql:", not just "postgresql:" - Mark Rotteveel
@MarkRotteveel I am not sure where I should make a change, should the configuration in .properties file be changed? jdbc.url=postgresql://localhost:5432/postgres to jdbc.url=jdbc:postgresql://localhost:5432/postgres. I still face the same issue.... :( - Sarronya
@MarkRotteveel thanks much ! It works fine now... - Sarronya

1 Answers

0
votes

You don't add url while filling the database details for postgresql. You instead do the following.

<bean id="dataSource" class="org.postgresql.xa.PGXADataSource">
    <property name="user" value="${username}" />
    <property name="password" value="${password}" />
    <property name="portNumber" value ="${portNumber}" />
    <property name="databaseName" value="${databaseName}" />
    <property name="serverName" value="${serverName}" />
</bean>

in your application context and

username=yourusername
password=******
portNumber=5432
databaseName=yourdb
serverName=localhost

in your property file.Later ofcourse you need to utilize the datasource bean. Look at the following, https://jdbc.postgresql.org/documentation/publicapi/org/postgresql/xa/PGXADataSource.html.