Intelij Java连接到mysql无法正常工作

问题描述

我想创建一个测试来检索/更新数据库条目。我是自动化测试中的新手,并且从未使用过与数据库的连接。 为此,我与用户和密码以及northwind数据库建立了本地连接。在sql Server Management Studio中一切正常 但在我尝试运行的以下测试中未建立与数据库的连接:

import java.sql.*;


public class Driver {

    public static void main(String[] args) {
        try {
                Connection myConn = DriverManager.getConnection("jdbc:MysqL://localhost:1433/northwind","coconut","P@ssw0rd123");
            // 1.Get a connection to database
            // Create a statement
            Statement myStmt = myConn.createStatement();
            // Execute sql query
            ResultSet myRs = myStmt.executeQuery("select * from Employees");
            // Process result set
            while (myRs.next()){
                System.out.println(myRs.getString("LastName"));
            }
        } catch (Exception exc) {
            exc.printstacktrace();
        }
    }
}

在检查了不同的教程之后,我在项目模块中配置了相应的jar: mysql-connector-java-8.0.21 sqljdbc4-2.0

sql Server配置管理器中,我喜欢sql在端口1433上侦听,这就是为什么我在连接URL中指定了此原因

运行上面的代码时,出现以下错误: com.MysqL.cj.jdbc.exceptions.CommunicationsException:通信链接失败

T

he 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:197)
    at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:677)
    at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:228)
    at Driver.main(Driver.java:8)
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:91)
    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:65)
    ... 9 more

退出代码0结束的过程

有什么问题的主意吗? 谢谢!

解决方法

可能出现以下问题,

首先在SQL Management Studio中测试您的连接。它可能无法启动。

jdbc:mysql://localhost:1433/Northwind"

1。确保您的端口号,用户名和密码正确

2尝试使用IP地址,而不要编写localhost。

3.DB服务器已用尽连接。