【问题标题】:Cannot create JDBC driver of class 'org.postgresql.Driver' for connect URL postgres://username:password@localhost:5432/dbname无法为连接 URL postgres://username:password@localhost:5432/dbname 创建类 'org.postgresql.Driver' 的 JDBC 驱动程序
【发布时间】:2016-10-19 19:23:35
【问题描述】:

我已经在 Tomcat 7 服务器上部署了两个 Spring roo 应用程序,两者都有数据库连接,同时访问我遇到的任何应用程序服务都低于异常

Cannot create JDBC driver of class 'org.postgresql.Driver' for connect URL 'postgres://username:password@localhost:5432/db'
java.sql.SQLException: No suitable driver
    at java.sql.DriverManager.getDriver(DriverManager.java:278)
    at org.apache.commons.dbcp.BasicDataSource.createConnectionFactory(BasicDataSource.java:1437)
    at org.apache.commons.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1371)
    at org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource.java:1044)
    at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:139)
    at org.hibernate.engine.jdbc.internal.JdbcServicesImpl$ConnectionProviderJdbcConnectionAccess.obtainConnection(JdbcServicesImpl.java:279)
    at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:124)
    at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:111)

请在下面找到数据库基础配置

应用 1

application-Context.xml

<bean class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" id="dataSource">
    <property name="driverClassName" value="${database.driverClassName}"/>
    <property name="url" value="${database.url}"/>
    <property name="username" value="${database.username}"/>
    <property name="password" value="${database.password}"/>
    <property name="testOnBorrow" value="true"/>
    <property name="testOnReturn" value="true"/>
    <property name="testWhileIdle" value="true"/>
    <property name="timeBetweenEvictionRunsMillis" value="1800000"/>
    <property name="numTestsPerEvictionRun" value="3"/>
    <property name="minEvictableIdleTimeMillis" value="1800000"/>
    <property name="validationQuery" value="SELECT version();"/>
</bean>
<bean class="org.springframework.orm.jpa.JpaTransactionManager" id="transactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<tx:annotation-driven mode="aspectj" transaction-manager="transactionManager"/>
<bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory">
    <property name="persistenceUnitName" value="persistenceUnit"/>
    <property name="dataSource" ref="dataSource"/>
</bean>

应用1.Database.properties

database.driverClassName=org.postgresql.Driver
database.url=jdbc\:postgresql\://localhost\:5432/db
database.username=username
database.password=password

应用 2

application-Context.xml

<bean class="java.net.URI" id="dbUrl">
    <constructor-arg value="postgres://username:password@localhost:5432/db"/>
</bean>
<bean class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" id="dataSource">
    <property name="driverClassName" value="org.postgresql.Driver"/>
    <property name="url" value="#{ 'jdbc:postgresql://' + @dbUrl.getHost() + ':' + @dbUrl.getPort() + @dbUrl.getPath() }"/>
    <property name="username" value="#{ @dbUrl.getUserInfo().split(':')[0] }"/>
    <property name="password" value="#{ @dbUrl.getUserInfo().split(':')[1] }"/>
    <property name="testOnBorrow" value="true"/>
    <property name="testOnReturn" value="true"/>
    <property name="testWhileIdle" value="true"/>
    <property name="timeBetweenEvictionRunsMillis" value="1800000"/>
    <property name="numTestsPerEvictionRun" value="3"/>
    <property name="minEvictableIdleTimeMillis" value="1800000"/>
    <property name="validationQuery" value="SELECT version();"/>
</bean>
<bean class="org.springframework.orm.jpa.JpaTransactionManager" id="transactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<tx:annotation-driven mode="aspectj" transaction-manager="transactionManager"/>
<util:properties id="regex.properties" location="classpath:META-INF/spring/regex.properties"/>
<bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory">
    <property name="persistenceUnitName" value="persistenceUnit"/>
    <property name="dataSource" ref="dataSource"/>
</bean>

请告诉我如何解决我的问题..

【问题讨论】:

  • 查看问题中的连接 URL。
  • 你的类路径中有 postgres 库吗?
  • 是的,f1sh,我的应用程序 2(旧)能够连接数据库,而第一个则不能
  • @Kayaman,编辑了错误的网址
  • @Mayur 这不是问题。您的应用找不到 postgres 驱动程序类。所以它不在应用程序的类路径中。

标签: java spring postgresql spring-roo


【解决方案1】:
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"     destroy-method="close">
    <property name="driverClass" value="org.postgresql.Driver" />
    <property name="jdbcUrl" value="jdbc:postgresql://localhost:5432/db" />
    <property name="user" value="user" />
    <property name="password" value="pass" />
    <!-- pool sizing -->
    <property name="initialPoolSize" value="3" />
    <property name="minPoolSize" value="6" />
    <property name="maxPoolSize" value="25" />
    <property name="acquireIncrement" value="3" />
    <property name="maxStatements" value="0" />
</bean>

我用过这个,效果很好

【讨论】:

  • 没错。当连接 URL 的格式正确时,它可以工作。
  • @Mayur 你在哪里编辑的,因为在我看到的所有地方都是错误的。
猜你喜欢
  • 2017-06-15
  • 1970-01-01
  • 2020-10-29
  • 2011-11-08
  • 2018-01-14
  • 2013-11-16
  • 2011-05-14
  • 2011-07-06
  • 2015-04-15
相关资源
最近更新 更多