关于#sql#的问题,如何解决?

######JDBC和MySQL连接

package com.djh.jdbc;

import org.junit.jupiter.api.Test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class JDBCDemo03 {


    /**
     *
     * @throws Exception
     */
    @Test

    public void testResultSet() throws  Exception {

        //2.获取连接
        String url = "jdbc:mysql://text?useSSL=false";
        String username="root";
        String password="djh1314..";
        Connection conn = DriverManager.getConnection(url, username, password);

        //接收用户输入的用户名和密码

        String name="zhangsan";
        String pwd="123";

        String sql="select * from user where  username='"+name+"' and password='"+pwd+"'";

        //获取stmt对象
        Statement stmt = conn.createStatement();
        //执行sql
        ResultSet rs = stmt.executeQuery(sql);

        //判断是否登录成功
        if(rs.next()){
            System.out.println("登录成功~");
        }else{
            System.out.println("登录失败~");
        }

        //7.释放资源
        conn.close();
        stmt.close();
        rs.close();
    }


}


Connected to the target VM, address: '127.0.0.1:55944', transport: 'socket'

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.

img

解决这个问题

你的 url里面没有主机地址和端口


String url = "jdbc:mysql://text?useSSL=false";

String url = "jdbc:mysql://localhost:3306/text?useSSL=false";

改成这样试试

jdbc:mysql://127.0.0.1:3306/text?useUnicode=true&characterEncoding=utf8&useSSL=false
这个试一试,如果不行就把127.0.0.1换成MySQL server的IP