【问题标题】:JDBC connection failure: java.sql.SQLException: Communication link failure: Bad handshakeJDBC 连接失败:java.sql.SQLException:通信链接失败:握手错误
【发布时间】:2018-03-05 07:31:49
【问题描述】:

我是 JDBC 的新手。我在尝试将我的 java 程序与 Mysql 数据库连接时遇到了一个 SQLException。

请在下面找到运行时错误的详细信息:

Exception in thread "main" java.sql.SQLException: Communication link failure: Bad handshake
    at com.mysql.jdbc.MysqlIO.init(Unknown Source)
    at com.mysql.jdbc.Connection.connectionInit(Unknown Source)
    at com.mysql.jdbc.jdbc2.Connection.connectionInit(Unknown Source)
    at com.mysql.jdbc.Driver.connect(Unknown Source)
    at java.sql.DriverManager.getConnection(DriverManager.java:664)
    at java.sql.DriverManager.getConnection(DriverManager.java:247)
    at databaseexample.DatabaseExample.connect(DatabaseExample.java:40)
    at databaseexample.DatabaseExample.main(DatabaseExample.java:26)
/home/sreejith/.cache/netbeans/8.2/executor-snippets/run.xml:53: Java returned: 1

Program Details :
package databaseexample;
import java.sql.*

;

public class DatabaseExample {

    static boolean flag = false;
    static String DBURL = "jdbc:mysql://localhost:3306/testdb";


    static String  CHECK_SQL_QUERY = "SELECT 1";
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws SQLException, ClassNotFoundException {
        // TODO code application logic here
        Class.forName("com.mysql.jdbc.Driver");
        flag =  connect();
        if(flag == true)
        {
            System.out.println("Connnected !");
        } else
        {
            System.out.println("not connected.");
        }



    }

    public static boolean connect() throws SQLException {
        Connection conn = DriverManager.getConnection(DBURL,"root","root");

        return true;
    }

}

我尝试 telnet 3306 端口,结果如下:

sreejith@desktop:~$ telnet localhost 3306
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
[
5.7.19-0ubuntu0.16.04.1*4` `iw2)Ml,L*
                                         Lmysql_native_password

!#08S01Got packets out of orderConnection closed by foreign host.

顺便说一句,我在 ubuntu 16.04 上运行。 在此先感谢:)

【问题讨论】:

标签: java mysql jdbc connection


【解决方案1】:

试试这个方法:

import java.sql.DriverManager;
import java.sql.SQLException;

public class Connection
{
    private static java.sql.Connection connection;

    public Connection(String serverAddress, String database, String user, String pass) throws ClassNotFoundException, SQLException
    {
        Class.forName("com.mysql.jdbc.Driver");
        String url = "jdbc:mysql://" + serverAddress + ":3306/" + database;
        connection = DriverManager.getConnection(url, user, pass);
    }

    public java.sql.Connection getConnection()
    {
        return connection;
    }
}

【讨论】:

  • 这是一个建议,而不是对 OP 发布的问题的答案,没有资格获得答案!
  • 这与 OP 使用的代码有何不同?提示:本质上是一样的。
  • 嗨Ramin..我已经尝试过你的代码,但我仍然面临同样的错误:(..有人可以解决这个问题吗?是数据库连接字符串的问题吗?
猜你喜欢
  • 2021-02-02
  • 1970-01-01
  • 2015-01-04
  • 2021-03-08
  • 2015-02-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-31
相关资源
最近更新 更多