出现的问题:
Cannot create JDBC driver of class 'oracle.jdbc.driver.OracleDriver' for connect URL 'jdbc:qfnu:thin:@127.0.0.1:1521:books'
java.sql.SQLException: No suitable driver
at java.sql.DriverManager.getDriver(Unknown Source)
at org.apache.commons.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1143)
at org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource.java:880)
at org.hibernate.service.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:141)
at org.hibernate.tool.hbm2ddl.SuppliedConnectionProviderConnectionHelper.prepare(SuppliedConnectionProviderConnectionHelper.java:51)
at org.hibernate.tool.hbm2ddl.SchemaUpdate.execute(SchemaUpdate.java:194)
at org.hibernate.tool.hbm2ddl.SchemaUpdate.execute(SchemaUpdate.java:178)
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:503)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1743)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1781)
代码如下:
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="/WEB-INF/jdbc.properties"/>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<!-- 指定连接数据库的驱动 -->
<property name="driverClassName" value="${jdbc.driverClassName}" />
<!-- 指定连接数据库的URL -->
<property name="url" value="${jdbc.url}" />
<!-- 指定连接数据库的用户名 -->
<property name="username" value="${jdbc.username}" />
<!-- 指定连接数据库的密码 -->
<property name="password" value="${jdbc.password}" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<prop key="current_session_context_class">thread</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.connection.autocommit">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
/WEB-INF/jdbc.properties
内容如下:
jdbc.properties:
#mysql测试成功
#jdbc.driverClassName=com.mysql.jdbc.Driver
# Database URL
#jdbc.url=jdbc:mysql://localhost:3306/books
# Database login information
#jdbc.username=root
#jdbc.password=root
#oracle测试不成功
#oracle
jdbc.driverClassName=oracle.jdbc.driver.OracleDriver
# Database URL
#注:qfnu是本机oracle的sid
jdbc.url=jdbc:qfnu:thin:@127.0.0.1:1521:books
# Database login information
jdbc.username=root
jdbc.password=root
ojdbc6.jar ojdbc.14.jar都试过了,还是报这个错误:
No suitable driver
at java.sql.DriverManager.getDriver(Unknown Source)
------解决方案--------------------
qfnu 是本机的sid,那books 应该是mysql的数据库名称吧。
本机的oracle的话,就直接
jdbc:oracle:thin:@127.0.0.1:1521:qfnu