1

以下是代码。

import java.sql.*;

public class ActivityReader {
    public static void main(String[] args) {
                      System.out.println("MySQL Connect Example.");
              Connection conn = null;
              String url = "jdbc:mysql://localhost:3306/";
              String dbName = "jdbctutorial";
              String driver = "com.mysql.jdbc.Driver";
              String userName = "root"; 
              String password = "password";
              try {
                  Class.forName("com.mysql.jdbc.Driver").newInstance();
                  conn = DriverManager.getConnection(url,userName,password);
                  System.out.println("Connected to the database");
                  conn.close();
                  System.out.println("Disconnected from database");
              } catch (Exception e) {
                      e.printStackTrace();
              }
    }

} 

我已经从这个 url在项目中添加了 mysql-connector-java-5.1.20-bin.jar 。Mysql 服务已经在运行。

mysql start/running, process 25326

MySQL 连接示例。

com.mysql.jdbc.exceptions.jdbc4.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.
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
    at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1116)
    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:348)
    at c

om.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2391)
    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2428)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2213)
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:797)
    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:389)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:305)
    at java.sql.DriverManager.getConnection(DriverManager.java:582)
    at java.sql.DriverManager.getConnection(DriverManager.java:185)
    at ActivityReader.main(ActivityReader.java:45)
Caused by: java.net.ConnectException: Connection refused
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:351)
    at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:213)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:200)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
    at java.net.Socket.connect(Socket.java:529)
    at java.net.Socket.connect(Socket.java:478)
    at java.net.Socket.<init>(Socket.java:375)
    at java.net.Socket.<init>(Socket.java:218)
    at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:257)
    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:298)
    ... 15 more

任何的想法.... ??????

4

5 回答 5

3

使用127.0.0.1instead连接localhost,也许是您系统上发生的事情的线索:

如果您告诉它连接到“localhost”,MySQL 将尝试连接到 unix 套接字。如果您告诉它连接到 127.0.0.1,您将强制它连接到网络套接字。因此,您可能已将 MySQL 配置为仅侦听网络套接字而不是文件系统套接字。

https://serverfault.com/questions/295285/mysql-cannot-connect-via-localhost-only-127-0-0-1

于 2012-06-20T11:08:35.400 回答
0

尝试

>telnet localhost 3306

您应该看到一些类似于以下的服务器响应

“5.5.19☺(:*b2p`p!☻ǧ4Oph7xxsV5C5mysql_native_password”

最有可能的问题将通过用 ip 替换主机名来解决,检查https://serverfault.com/a/90055

于 2012-06-20T10:50:02.473 回答
0

我认为你没有在你的连接 URL 中指定你的数据库名称
试试这个
conn = DriverManager.getConnection(url +"/"+ dbName ,userName,password);
并确保您已创建具有相同名称的数据库。

于 2014-01-16T12:28:39.583 回答
0

我在 VPS linux 服务器上遇到了同样的问题。我通过用真实 IP 地址替换 localhost 或 127.0.0.1 来解决它。

于 2018-04-08T10:31:03.483 回答
0

如果您尝试在 Linux 机器或 Windows Server 上运行您的代码。

您可以将服务器从'localhost' to specific IP 'xx.xx.xx.xx'

它应该可以正常工作。

于 2020-09-23T12:43:15.240 回答