spring hibernate c3p0 整合连接不上数据库

可能原因

1,驱动配置有误:driver=com.mysql.jdbc.Driver
2,数据库连接地址有误:url=jdbc:mysql://localhost:3306/test?3useUnicode=true&characterEncoding=utf8
3,密码或帐号有误:username=root
      password=root

4,数据库未启动或无权访问

5,项目未引入对应的驱动jar包mysql-connector-java-5.1.6-bin.jar

6,mysql root没有远程访问的权限,需要增加权限

检查连接配置:db.properties

#data
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/数据库名称?characterEncoding=utf8
jdbc.username=用户名
jdbc.password=密码

# c3p0
c3p0.minPoolSize=3
c3p0.maxPoolSize=50
c3p0.initialPoolSize=5
c3p0.maxIdleTime=120
c3p0.acquireIncrement=5
c3p0.idleConnectionTestPeriod=60

#hibernate

hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect
hibernate.show_sql=true
hibernate.format_sql=true
hibernate.hbm2ddl.auto=update
hibernate.current_session_context_class=org.springframework.orm.hibernate5.SpringSessionContext
hibernate.connection.autocommit=true
hibernate.javax.persistence.validation.mode=none

注意:每一行后面不可以有空格

检查spring配置信息如下:

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
		<property name="locations">
			<list>
				<value>classpath:db/*.properties</value>
			</list>
		</property>
	</bean>
	<!-- c3p0数据源 -->
	<bean id="dataSource" 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="minPoolSize" value="${c3p0.minPoolSize}"/>  
        <property name="maxPoolSize" value="${c3p0.maxPoolSize}"/>  
        <property name="initialPoolSize" value="${c3p0.initialPoolSize}"/>  
        <property name="maxIdleTime" value="${c3p0.maxIdleTime}"/>  
        <property name="acquireIncrement" value="${c3p0.acquireIncrement}"/>  
        
        <property name="idleConnectionTestPeriod" value="${c3p0.idleConnectionTestPeriod}"/>  
	</bean>
	<!-- 配置sessionFactory -->
	<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
		<property name="dataSource" ref="dataSource"></property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">${hibernate.dialect}</prop>
				<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
				<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
				<prop key="hiberate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
				<prop key="hibernate.connection.autocommit">${hibernate.connection.autocommit}</prop>
				<prop key="hiberate.javax.persistence.validation.mode">${hibernate.javax.persistence.validation.mode}</prop>
			</props>
		</property>
		<!-- 扫描实体类包 -->
		<property name="packagesToScan" value="com.evaluationmanager.entiy"></property>
	</bean>
	
	<!-- 配置hibernatTemplate 模板 -->
	 <bean id="hibernatTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
	 	<property name="sessionFactory" ref="sessionFactory"></property>
	 </bean>
	 <!-- 配置事务管理 --> 
	 <!-- transationManager -->
	 <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
	 	<property name="sessionFactory" ref="sessionFactory"></property>
	 </bean>
	 <!-- 开启事务注解 -->
	 <tx:annotation-driven transaction-manager="transactionManager"/>

 

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页