Unable to connect JDBC to MySQL (using Java, IntelliJ and Linux)

I am having problems trying to get a database connection using the following code:

try { Class.forName("com.mysql.jdbc.Driver"); Properties p = new Properties(); p.put("user", user_name); p.put("password", password); connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1/jsp_test", p); } catch (SQLException ex) { // handle any errors ex.printStackTrace(); System.out.println("SQLException: " + ex.getMessage()); System.out.println("SQLState: " + ex.getSQLState()); System.out.println("VendorError: " + ex.getErrorCode()); return false; } catch (ClassNotFoundException e) { e.printStackTrace(); } 

Error message displayed:

/ USR / Library / JVM / Java-6-OpenJDK / bin / Java -Didea.launcher.port = 7532 -Didea.launcher.bin.path = / usr / bin / idea / bin -Dfile.encoding = UTF-8 - classpath / usr / lib / jvm / java-6-openjdk / jre / lib / jce.jar: / usr / lib / jvm / java-6-openjdk / jre / lib / about. jar: /usr/lib/jvm/java-6-openjdk/jre/lib/resources.jar: /usr/lib/jvm/java-6-openjdk/jre/lib/management-agent.jar: / USR / Library /jvm/java-6-openjdk/jre/lib/jsse.jar:/usr/lib/jvm/java-6-openjdk/jre/lib/charsets.jar:/usr/lib/jvm/java-6-openjdk /jre/lib/rt.jar:/usr/lib/jvm/java-6-openjdk/jre/lib/ext/localedata.jar:/usr/lib/jvm/java-6-openjdk/jre/lib/ext /sunjce_provider.jar:/usr/lib/jvm/java-6-openjdk/jre/lib/ext/sunpkcs11.jar:/usr/lib/jvm/java-6-openjdk/jre/lib/ext/dnsns.jar : / home / going to bed / Java Projects / db_demo / out / production / db_demo: /opt/java/jre/lib/ext/mysql-connector-java-5.1.10-bin.jar: / usr / bin / idea /lib/idea_rt.jar com.intellij.rt.execution.application.AppMain Main com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communication link error

The last packet the server sent successfully was 0 milliseconds ago. The driver did not receive any packets from the server. in sun.reflect.NativeConstructorAccessorImpl.newInstance0 (Native Method) with sun.reflect.NativeConstructorAccessorImpl.newInstance (NativeConstructorAccessorImpl.java:57) in sun.reflect.DelegatingConstruor. javrutlava.java .newInstance (Constructor.java//32) in com.mysql.jdbc.Util.handleNewInstance (Util.java:406) in com.mysql.jdbc.SQLError.createCommunicationsException (SQLError.java:1074) in com.mysql.jdbc. ConnectionImpl.createNewIO (ConnectionImpl.java:2214) in com.mysql.jdbc.ConnectionImpl. (ConnectionImpl.java:781) in com.mysql.jdbc.JDBC4Connection. (JDBC4Connection.java:46) in sun.reflect.NativeConstructorAccessorImpl.newInstance0 (Native Method) when sun.reflect.NativeConstructorAccessorImpl.newInstance (NativeConstructorAccessorImpl.java:57) in sun.reflect.Delegatingorplpllectlectstructorlectstructorlectstructorlectstructorlectstructorlectstructorstructor java.lang.reflect.Constructor.newInstance (Constructor.javaβˆ—32) in com.mysql.jdbc.Util.handleNewInstance (Util.java:406) in com.mysql.jdbc.ConnectionImpl.getInstance (ConnectionImpl.javahaps52) in com.mysql.jdbc.NonRegisteringDriver.connect (NonRegisteringDriver.java:284) in java.sql.DriverManager.getConnection (DriverManager.java:620) in java.sql.DriverManager.getConnection (DriverManager.java:169). Database.connect (Database.java:80) on Main.main (Main.java:13) in sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method) with sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:57) in sun .reflect.DelegatingMethodAccessorImp l.invoke (DelegatingMethodAccessorImpl.java:43) in java.lang.reflect.Method.invoke (Method.java:616) in com.intellij.rt.execution.application.AppMain.main (AppMain.java:110) Called: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communication link error

The last packet the server sent successfully was 0 milliseconds ago. The driver did not receive any packets from the server. in sun.reflect.NativeConstructorAccessorImpl.newInstance0 (Native Method) with sun.reflect.NativeConstructorAccessorImpl.newInstance (NativeConstructorAccessorImpl.java:57) in sun.reflect.DelegatingConstruor. javrutlava.java .newInstance (Constructor.java//32) in com.mysql.jdbc.Util.handleNewInstance (Util.java:406) in com.mysql.jdbc.SQLError.createCommunicationsException (SQLError.java:1074) in com.mysql.jdbc. MysqlIO. (MysqlIO.javahaps43) in com.mysql.jdbc.ConnectionImpl.createNewIO (ConnectionImpl.java:2137) ... 18 more Caused: java.net.ConnectException: connection refused java.net.PlainSocketImpl.socketConnect (Native Method) when java.net.AbstractPlainSocketImpl.doConnect (AbstractPlainSocketImpl.java data 10) in java.net.AbstractPlainSocketImpl.connectToAddress (AbstractPlainSocketImpl.java:176) in java.net.AbstractPlainSocketImpl.connect (AbstractPlain.net16lava.java. SocksSocketImpl.connect (SocksSocketImpl.javahaps84) in java.net.Socket.connect (Socket.java//42) in java.net.Socket.connect (Socket.java:492) in java.net.Socket. (Socket.javahaps89) in java.net.Socket. (Socket.java:232) in com.mysql.jdbc.StandardSocketFactory.connect (StandardSocketFactory.java:253) in com.mysql.jdbc.MysqlIO. (MysqlIO.java:292) ... 19 more SQLException: Communication error communication channel

The last packet the server sent successfully was 0 milliseconds ago. The driver did not receive any packets from the server. SQLState: 08S01 VendorError: 0

Process terminated by exit code 0

I literally don't know how to fix this error message. The database exists. Username and password exist. I have not added any tables to the database at present, but I do not think this could be a problem, since I only create the connection in the end ...

I can provide additional information if necessary. I feel like I've tried a lot. Does anyone know of any ways to get more information on how and why this fails?

Thanks for your help!:)

+5
source share
4 answers

From your connection string, this database should be on the local computer. Can you try running this command to make sure the socket is open for connections?

telnet 127.0.0.1 3306

and make sure it connects? You may not have configured your mysql instance to listen for connections from this machine or through this interface. If the connection fails, you need to change the mysql configuration, for example:

http://www.cyberciti.biz/tips/how-do-i-enable-remote-access-to-mysql-database-server.html

+6
source

I had the same symptoms, but for me the solution was to change the binding address to 0.0.0.0 in /etc/mysql/my.cnf

(Just post this to others looking for an answer to this question)

+1
source

Instead, you can try:

 connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/jsp_test","user_name","password"); 
+1
source

you can try one of the following

 "jdbc:mysql://localhost:3306/jsp_test","username","password" 

or

 "jdbc:mysql://'your machines ip (without quote)':3306/jsp_test","username","password" 

or

 "jdbc:mysql://127.0.0.1:3306/jsp_test","username","password" 

as a connection string.

+1
source

Source: https://habr.com/ru/post/896013/


All Articles