java.net.ConnectException

java.net.ConnectException: Connection refused: connect.问题,求大神解救,跟着视频打的,但通用的方法运行失败

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

Last packet sent to the server was 0 ms ago.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1074)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2120)
at com.mysql.jdbc.ConnectionImpl.(ConnectionImpl.java:723)
at com.mysql.jdbc.JDBC4Connection.(JDBC4Connection.java:46)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:302)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:282)
at com.jdbc.JDBCtest.getConnection(JDBCtest.java:70)
at com.jdbc.JDBCtest.testGetConnection(JDBCtest.java:77)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

Last packet sent to the server was 0 ms ago.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1074)
at com.mysql.jdbc.MysqlIO.(MysqlIO.java:335)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2043)
... 34 more
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.(Unknown Source)
at java.net.Socket.(Unknown Source)
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:253)
at com.mysql.jdbc.MysqlIO.(MysqlIO.java:284)
... 35 more

源代码:
package com.jdbc;

import static org.junit.Assert.*;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

import org.junit.Test;

import com.mysql.jdbc.Driver;

public class JDBCtest {
@Test
public void testDriver() throws SQLException {
//1.创建一个Driver实现类的对象
Driver driver = new com.mysql.jdbc.Driver();

    //2.准备连接数据库的基本信息:url,user,password
    String url = "jdbc:mysql://localhost:3306/test"; //jdbc:子协议://数据库所在的主机ip:端口号/数据库名
    Properties info = new Properties();
    info.put("user", "root");
    info.put("password", "1230");

    //3.调用Driver接口的connection(url,info)获取数据库资源
    Connection connection = driver.connect(url, info);
    System.out.println(connection);
}

/**
 * 编写一个通用的方法,在不修改源程序的情况下,可以获取任何数据库的连接
 * 解决方案:把数据库驱动Driver实现类的全类名、url、user、password放入一个
 * 配置文件中,通过修改配置文件的方式实现和具体数据库解耦。
 * @throws Exception 
 */

public Connection getConnection() throws Exception{
    String driverClass = null;
    String jdbcUrl = null;
    String user = null;
    String password = null;

    //读取类路径下的jdbc.properties文件
    //输入流
    InputStream in = 
            getClass().getClassLoader().getResourceAsStream("jdbc.properties");
    Properties properties = new Properties();
    properties.load(in);
    driverClass = properties.getProperty("driver");
    jdbcUrl = properties.getProperty("jdbcUrl");
    user = properties.getProperty("user");
    password = properties.getProperty("password");

    Driver driver = (Driver) Class.forName(driverClass).newInstance();

    Properties info = new Properties();
    info.put("user",user);
    info.put("password",password);
    Connection connection = driver.connect(jdbcUrl,info);

    return connection;
}

@Test
public void testGetConnection() throws Exception{
    System.out.println(getConnection());
}

}

jdbc.properties文件:
driver=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql://localhost:3306/test
user=root
password=1230

7个回答

在mysql里面添加权限,允许localhost链接数据库

Janetyuu
Janetyuu 我是用eclipse连接的,应该不关mysql的问题吧?
接近 3 年之前 回复

你用navicat去连接该数据库能够连上不?

Janetyuu
Janetyuu 可以啊,就是用eclipse就连不上,一开始可以的
接近 3 年之前 回复

你先创建一个连接 看看连接是否成功嘛 Navicat里面可以连接 就没问题了 用户名密码数据库名字相对应就差不多了

在数据库中查询下是否有访问权限select user,host from mysql.user;查看是否有root localhost这样的一条结果,如果没有执行命令
grant all privileges on test.* to root@'localhost'试下

图片说明,两个测试,不管是读取文件,还是直接传值都没有问题啊,你的配置文件啊,你的用户赋予了那些权限啊。

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

确定网络是通的。看一看你网络有没有问题

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问