配置文件<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"><beans>
<!-- 加载  properties 文件 -->
<bean id="placeholderConfig"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>/properties/OracleDataSoures.properties</value>
</property>
</bean>



<!-- 数据源 -->
<bean id="myDataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
       <property name="driverClassName">
<value>${datasource.driverClassName}</value>
</property>
<property name="url">
<value>${datasource.url}</value>
</property>
<property name="username">
<value>${datasource.username}</value>
</property>
<property name="password">
<value>${datasource.password}</value>
</property>
    </bean>
<!-- 数据源 --><!--数据库会话工厂 -->    
<bean id="mySessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"
singleton="true">
<property name="dataSource">
<ref local="myDataSource" />
</property>
<property name="mappingResources">
<list>

<value>com/hibernate/Dept.hbm.xml</value>

</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
${hibernate.dialect}
</prop>
<prop key="hibernate.show_sql">
${hibernate.show_sql}
</prop>
<!--<prop key="hibernate.jdbc.fetch_size">
${hibernate.jdbc.fetch_size}
</prop>
<prop key="hibernate.jdbc.batch_size">
${hibernate.jdbc.batch_size}
</prop>
<prop key="hibernate.connection.release_mode">
${hibernate.connection.release_mode}
</prop>
<prop key='hibernate.query.factory_class'>
${hibernate.query.factory_class}
</prop>
--></props>
</property>
</bean>
<!--数据库会话工厂 -->
<!-- 事务管理器 -->
<bean id="myTransactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="mySessionFactory" />
</property>
</bean>
<!-- 事务管理器 -->
<!-- Spring 事务管理器代理 -->
<bean id="myTransactionProxyFactoryBean" lazy-init="true"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref bean="myTransactionManager" />
</property>
<property name="transactionAttributes">
<props>
<prop key="create*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="delete*">PROPAGATION_REQUIRED</prop>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="move*">PROPAGATION_REQUIRED</prop>
<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
<!-- Spring 事务管理器代理 --><!-- 切面 -->
<bean id="userImp" class="com.dao.user.impl.UserImpl">
<property name="sessionFactory">
    <ref local="mySessionFactory" />
   </property>
</bean>
<!-- 切面 -->
<!-- 代理 --><bean id="user_proxy" parent="myTransactionProxyFactoryBean">
    <property name="target">
      <ref local="userImp"/>
    </property>
  </bean>
<!-- 代理 --></beans>
我想测试是否连接上数据库
package com;import java.sql.SQLException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {

public static void main(String[] args) throws SQLException {
ApplicationContext   context   =   new   ClassPathXmlApplicationContext("applicationContext.xml"); 
org.springframework.jdbc.datasource.DriverManagerDataSource temp = (org.springframework.jdbc.datasource.DriverManagerDataSource) context.getBean("myDataSource");
System.out.println(temp.getConnection());


}}
报错
Exception in thread "main" java.sql.SQLException: No suitable driver
at java.sql.DriverManager.getConnection(DriverManager.java:545)
at java.sql.DriverManager.getConnection(DriverManager.java:140)
at org.springframework.jdbc.datasource.DriverManagerDataSource.getConnectionFromDriverManager(DriverManagerDataSource.java:291)
at org.springframework.jdbc.datasource.DriverManagerDataSource.getConnectionFromDriverManager(DriverManagerDataSource.java:277)
at org.springframework.jdbc.datasource.DriverManagerDataSource.getConnectionFromDriverManager(DriverManagerDataSource.java:259)
at org.springframework.jdbc.datasource.DriverManagerDataSource.getConnection(DriverManagerDataSource.java:240)
at com.Test.main(Test.java:26)我的驱动包导入到了lib文件下,放在了Classpath请问是什么原因,困惑死我了,麻烦那位大侠看下谢谢

解决方案 »

  1.   

    关键部分全部都在properties 配置文件里。。竟然不贴出配置文件的配置内容。。 
      

  2.   

    不好意思,因为我测试过properties  没有问题datasource.driverClassName=oracle.jdbc.driver.OracleDriver
    datasource.url=jdbc:oralce:thin:@localhost:1521:luowei
    datasource.username=scott
    datasource.password=tigerdatasource.maxActive=10
    datasource.maxIdle=2
    datasource.maxWait=12000
    datasource.whenExhaustedAction=1
    datasource.validationQuery=select 1 from dual
    datasource.testOnBorrow=true
    datasource.testOnReturn=falsec3p0.acquireIncrement=3
    c3p0.initialPoolSize=3
    c3p0.idleConnectionTestPeriod=900
    c3p0.minPoolSize=2
    c3p0.maxPoolSize=50
    c3p0.maxStatements=100
    c3p0.numHelperThreads=10
    c3p0.maxIdleTime=600hibernate.dialect=org.hibernate.dialect.Oracle9Dialecthibernate.jdbc.batch_size=50
    hibernate.jdbc.fetch_size=50
    hibernate.show_sql=true
    hibernate.connection.release_mode=after_transaction
    hibernate.query.factory_class=org.hibernate.hql.classic.ClassicQueryTranslatorFactory,
      

  3.   

    像是Oracle驱动的问题,再确认一边路径,驱动包。
      

  4.   

    确认是 classes12.jar 这个包放到lib目录中了么
      

  5.   

    userImp你已经注入了mySessionFactory,在这个类中extends hibernatedaosupport,用,this.gethibernatetemplate.excute()方法测试下
      

  6.   

    把驱动包复制到项目的LIB下就OK啦
      

  7.   

    其实你数据源 可以直接写出来的 没必要撒都到资源文件里面去读比如
    <!-- Generated by MyEclipse Hibernate Tools.                   -->
    <hibernate-configuration> <session-factory>
    <property name="dialect">
    org.hibernate.dialect.OracleDialect
    </property>
    <property name="connection.url">
    jdbc:oracle:thin:@10.241.96.123:1521:ORA9I
    </property>
    <property name="connection.username">elplat</property>
    <property name="connection.password">elplat_168</property>
    <property name="connection.driver_class">
    oracle.jdbc.driver.OracleDriver
    </property>
    <property name="myeclipse.connection.profile">ora9i</property>
    <!-- Enable Hibernate's automatic session context management -->
    <property name="current_session_context_class">thread</property>
    <property name="show_sql">true</property>
      

  8.   

    15楼说得不错,直接写到Spring配置文件里面就可以了,没必要单独放一个文件里面的,放了还有可能出现意想不到的错误
      

  9.   

    先直接在代码中连接数据库,不要通过spring 试试
      

  10.   

    先把数据库的配置写到spring里 试试
    不行就看看你的驱动包
    我有次也是明明引了还是找不到包 最后一看那个包是0K的 
      

  11.   

    看看oracle驱动jar,换个最新的,是否加载了