简体   繁体   English

MySQL的JDBC连接问题

[英]JDBC Connection issues with MySQL

I've been looking over StackOverflow for the last two days for similar problems and have found many people with the same issue, however, nothing suggested has worked for me so I figured I would post my exact question here in case I'm missing something obvious. 最近两天,我一直在寻找StackOverflow来解决类似问题,并且发现很多人都遇到了同样的问题,但是,没有任何建议对我有用,所以我认为我会在此处发布确切的问题,以防万一我遗漏了一些东西明显。 I've noticed the connection refused causation, however, I know exactly what my username and password is as well as the dbname, so I know it's not an authentication problem. 我注意到连接被拒绝的原因,但是,我确切知道我的用户名和密码以及dbname是什么,所以我知道这不是身份验证问题。

After much frustration, I have found a class that simply checks the version of MySQL running on my machine. 沮丧之后,我发现了一个仅检查机器上运行的MySQL版本的类。 I am running MySQL using XAMPP as well. 我也使用XAMPP运行MySQL。 However, no matter what I try to do, I get the following error: 但是,无论我尝试做什么,都会出现以下错误:

Apr 26, 2014 7:29:37 PM Version main
SEVERE: 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.
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:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:409)
    at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1127)
    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:356)
    at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2502)
    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2539)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2321)
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:832)
    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:46)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:409)
    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:417)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:344)
    at java.sql.DriverManager.getConnection(DriverManager.java:579)
    at java.sql.DriverManager.getConnection(DriverManager.java:221)
    at Version.main(Version.java:23)
Caused by: java.net.ConnectException: Connection refused
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:391)
    at java.net.Socket.connect(Socket.java:579)
    at java.net.Socket.connect(Socket.java:528)
    at java.net.Socket.<init>(Socket.java:425)
    at java.net.Socket.<init>(Socket.java:241)
    at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:258)
    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:306)
    ... 15 more

And here is my Version class: 这是我的Version类:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Version {

    public static void main(String[] args) {

        Connection con = null;
        Statement st = null;
        ResultSet rs = null;

        String url = "jdbc:mysql://localhost:3306/dbname";
        String user = "username";
        String password = "password";

        try {
            con = DriverManager.getConnection(url, user, password);
            st = con.createStatement();
            rs = st.executeQuery("SELECT VERSION()");

            if (rs.next()) {
                System.out.println(rs.getString(1));
            }

        } catch (SQLException ex) {
            Logger lgr = Logger.getLogger(Version.class.getName());
            lgr.log(Level.SEVERE, ex.getMessage(), ex);

        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (st != null) {
                    st.close();
                }
                if (con != null) {
                    con.close();
                }

            } catch (SQLException ex) {
                Logger lgr = Logger.getLogger(Version.class.getName());
                lgr.log(Level.WARNING, ex.getMessage(), ex);
            }
        }
    }
}

I was able to get this working. 我能够使它工作。

I had to reinstall XAMPP and recreate the Database from scratch from the Exported SQL. 我必须重新安装XAMPP并从Exported SQL重新创建数据库。

You have no idea how annoyed I was to do this. 你不知道我这样做有多烦。 Lots of wasted time. 很多浪费的时间。 Thanks for the help. 谢谢您的帮助。

On ubuntu i work with MySQL and tried to edit a file with terminal 在Ubuntu上,我使用MySQL,并尝试使用终端编辑文件
sudo nano /etc/mysql/my.cnf 须藤nano /etc/mysql/my.cnf
find the line that contains: 找到包含以下内容的行:
bind-address 绑定地址
just edit this line 只需编辑此行
bind-address = 0.0.0.0 绑定地址= 0.0.0.0
save the file and restart mysql with 保存文件并重新启动mysql
sudo service mysql restart and try again sudo服务mysql重新启动,然后重试

it worked for me, good luck! 它对我有用,祝你好运!

You missed some code pal! 您错过了一些代码朋友! try adding Class.forName(com.mysql.jdbc.Driver); 尝试添加Class.forName(com.mysql.jdbc.Driver); in your try catch block. 在您的try catch块中。 hope it helps 希望能帮助到你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM