Reputation: 23
I'm trying to connect MySql and Java, but I get this error from this code.
Code:
public class ConnessioneMySql {
public static void main(String[] args) throws SQLException {
Connection cn;
Statement st;
ResultSet rs;
String sql;
cn = DriverManager.getConnection("jdbc:mysql://localhost:3306/TelegramBot", "root", "password");
sql = "SELECT * FROM Utente;";
try {
st = cn.createStatement();
rs = st.executeQuery(sql);
while (rs.next() == true) {
System.out.println(rs.getString("IdUtente") +
"\t" + rs.getString("CAPITALE")
+ "\t" + rs.getString("N_Inviti")
+ "\t" + rs.getString("Guadagno")
+ "\t" + rs.getString("DataUltimoAggiornamento"));
}
} catch (SQLException e) {
System.out.println("errore:" + e.getMessage());
}
cn.close();
}
}
Error:
Exception in thread "main" com.mysql.cj.jdbc.exceptions.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 com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:174)
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64)
at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:836)
at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:456)
at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:246)
at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:198)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:677)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:228)
at ConnessioneMySql.main(ConnessioneMySql.java:11)
Caused by: com.mysql.cj.exceptions.CJCommunicationsException: 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 java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:500)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:481)
at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61)
at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:105)
at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:151)
at com.mysql.cj.exceptions.ExceptionFactory.createCommunicationsException(ExceptionFactory.java:167)
at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:89)
at com.mysql.cj.NativeSession.connect(NativeSession.java:144)
at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:956)
at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:826)
... 6 more
Caused by: java.net.ConnectException: Connection refused: connect
at java.base/sun.nio.ch.Net.connect0(Native Method)
at java.base/sun.nio.ch.Net.connect(Net.java:503)
at java.base/sun.nio.ch.Net.connect(Net.java:492)
at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:588)
at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:333)
at java.base/java.net.Socket.connect(Socket.java:648)
at com.mysql.cj.protocol.StandardSocketFactory.connect(StandardSocketFactory.java:155)
at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:63)
... 9 more
Process finished with exit code 1
I dont actualy use a localhost, but a vps i just replace the real addres with "localhost".
MySql is running.
MySql version:8.0.21
mysql-connector-java version:8.0.22
Maybe is a problem of firewall or something...
Upvotes: 0
Views: 1384
Reputation: 23
At the end i discovered that it was a problem of firewalls, and i resolved it in my vps.
Upvotes: 1
Reputation: 37
2.Make sure the MySql service is running. you can do this by checking "services" in the task manager
cn = DriverManager.getConnection("jdbc:mysql://localhost:3306/TelegramBot", "root", "password");
with this:
String url = "jdbc:mysql://localhost:3306/TelegramBot?useUnicode=true&useJDBCCompliantTimeZoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC";
cn = DriverManager.getConnection(url, "root", "password");
Upvotes: 0