使用Spring JdbcTemplate报异常Communications link failure

peter20091001 2017-06-03 05:32:17
spring版本:3.2.9
spring.xml
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/zhongkai?useUnicode=true&characterEncoding=utf-8&autoReconnect=true&failOverReadOnly=false"></property>
<property name="username" value="root"></property>
<property name="password" value="zhongkai"></property>
</bean>
<!-- 配置jdbcTemplate -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<constructor-arg><ref bean="dataSource"/></constructor-arg>
</bean>
Java代码:
public class DBTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
JdbcTemplate temp = (JdbcTemplate)context.getBean("jdbcTemplate");
String sql = "select * from company";
List list = (List)temp.queryForList(sql);
System.out.println(list);
}
}
运行报异常:
Exception in thread "main" org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up.
Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
我在mysql的my.ini中设置了
wait_timeout = 172800
interactive-timeout = 172800
还是不行。求高手指教!
...全文
176 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧