com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to
程序员文章站
2022-05-01 16:25:36
...
项目简单的用jdbc访问数据库是报错:
代码如下:
public class JdbcMain {
public static void main(String[] args) throws Exception {
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/local","root","******");
connection.setAutoCommit(false);
String sql = "insert into user_info values (?,?,?)";
PreparedStatement preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1,3);
preparedStatement.setString(2,"用户3");
preparedStatement.setInt(3,31);
try {
System.out.println("开始执行");
preparedStatement.executeUpdate();
connection.commit();
System.out.println("事务提交结束");
}catch (Exception e){
connection.rollback();
e.printStackTrace();
}finally {
if(preparedStatement!=null){
preparedStatement.close();
}
if(connection!=null){
connection.close();
}
}
}
}
看了下使用的依赖是:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.29</version>
</dependency>
尝试升级了版本,换成:
<!-- mysql驱动包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
运行成功
当然这个异常也有可能是其他问题造成的,这里只是我分享的其中一个问题的解决方案而已哈,不妨一试。
推荐阅读
-
Myeclipse链接Oracle等数据库时lo exception: The Network Adapter could not establish the connection
-
解决Mono For Android不能编译问题:Could not create the Android package
-
解决Mono For Android不能编译问题:Could not create the Android package
-
vsftp上传553 Could not create file错误解决
-
Apache No space left on device: mod_rewrite: could not create rewrite_log_lock Configuration Failed
-
could not create the java virtual machine解决办法
-
oracle sql developer 出现 : 适配器无法建立连接问题解决方案 The Network Adapter could not establish the connection
-
关于 The Network Adapter could not establish the connection的问题
-
Tomcat配置过程中的问题:端口号被占用+Could not create java virtual machine+Startup.bat开始闪退
-
Could not get JDBC connection