gpt4 book ai didi

java - com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:通信链接失败

转载 作者:行者123 更新时间:2023-11-29 16:16:12 28 4
gpt4 key购买 nike

我正在努力让数据库与Java程序进行通讯。

有人可以使用JDBC给我一个快速而肮脏的示例程序吗?

我收到一个相当惊人的错误:

Exception in thread "main" com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure 
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:409)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1122)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2260)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:787)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:49)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:409)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:357)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:285)
at java.sql.DriverManager.getConnection(DriverManager.java:582)
at java.sql.DriverManager.getConnection(DriverManager.java:207)
at SqlTest.main(SqlTest.java:22)
Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:409)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1122)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:344)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2181)
... 12 more
Caused by: java.net.ConnectException: Connection refused
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:432)
at java.net.Socket.connect(Socket.java:529)
at java.net.Socket.connect(Socket.java:478)
at java.net.Socket.<init>(Socket.java:375)
at java.net.Socket.<init>(Socket.java:218)
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:256)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:293)
... 13 more


测试文件内容:

import com.mysql.jdbc.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class SqlTest {

public static void main(String [] args) throws Exception {
// Class.forName( "com.mysql.jdbc.Driver" ); // do this in init
// // edit the jdbc url
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/projects?user=user1&password=123");
// Statement st = conn.createStatement();
// ResultSet rs = st.executeQuery( "select * from table" );

System.out.println("Connected?");
}
}

最佳答案

所以,你有一个


  com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:通信链接失败
  java.net.ConnectException:连接被拒绝


我引用了this answer,其中还包含一个逐步的MySQL + JDBC教程:


  如果获得SQLException: Connection refusedConnection timed out或MySQL特定的CommunicationsException:
Communications link failure
,则意味着该数据库根本无法访问。可能有以下一种或多种原因:
  
  
  JDBC URL中的IP地址或主机名错误。
  本地DNS服务器无法识别JDBC URL中的主机名。
  JDBC URL中的端口号丢失或错误。
  数据库服务器已关闭。
  DB服务器不接受TCP / IP连接。
  数据库服务器已用尽连接。
  Java和DB之间的某种情况阻止了连接,例如防火墙或代理。
  
  
  
  要解决一个或另一个,请遵循以下建议:
  
  
  用ping验证并测试它们。
  刷新DNS或在JDBC URL中使用IP地址。
  根据MySQL DB的my.cnf进行验证。
  启动数据库。
  验证是否在没有--skip-networking option的情况下启动mysqld。
  重新启动数据库,并相应地修复代码,以关闭finally中的连接。
  禁用防火墙和/或配置防火墙/代理以允许/转发端口。
  
  


也可以看看:


How should I connect to JDBC database / datasource in a servlet based application?
Is it safe to use a static java.sql.Connection instance in a multithreaded system?

关于java - com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:通信链接失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54777656/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com