日期:2014-05-19  浏览次数:20768 次

spring 控制台程序
不是web程序
 applicationContext.xml配置好了一些必要的bean,比如datasource等;
  ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");  
这个怎么获得数据库连接 
用注解还是什么?有详细示例吗

------解决方案--------------------
datasource不就是要配置数据源的吗?你里面配成啥了?
这是我的datasource配置你可以看一下:

beans.xml
XML code

<bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location">
            <value>classpath:jdbc.properties</value>
        </property>
    </bean>
    
    <bean id="dataSource" destroy-method="close" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driverClassName}" />
        <property name="jdbcUrl" value="${jdbc.url}" />
        <property name="user" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
        <property name="acquireIncrement" value="${c3p0.acquireIncrement}"/>
        <!-- <property name="initialPoolSize" value="${c3p0.initalPoolSize}"/> -->
        <property name="minPoolSize" value="${c3p0.minPoolSize}"/>
        <property name="maxPoolSize" value="${c3p0.maxPoolSize}"/>
        <property name="maxStatements" value="${c3p0.maxStatements}"/>
        <property name="numHelperThreads" value="${c3p0.numHelperThreads}"/>
        <property name="maxIdleTime" value="${c3p0.maxIdleTime}"/>
        
    </bean>