与MySQL的JDBC连接问题

时间:2014-04-27 00:38:27

标签: java mysql jdbc connection

过去两天我一直在寻找类似问题的StackOverflow,并且发现很多人都有同样的问题,但是,没有任何建议对我有用,所以我想我会在这里发布我的确切问题以防万一我错过了一些明显的东西。我注意到连接拒绝了因果关系,但是,我确切知道我的用户名和密码是什么以及dbname,所以我知道它不是身份验证问题。

经过多次挫折之后,我找到了一个只检查我机器上运行的MySQL版本的类。我也使用XAMPP运行MySQL。但是,无论我尝试做什么,我都会收到以下错误:

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

这是我的版本类:

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);
            }
        }
    }
}

3 个答案:

答案 0 :(得分:1)

我能够让这个工作。

我不得不重新安装XAMPP并从Exported SQL重新创建数据库。

你不知道我是多么恼火。浪费了很多时间。谢谢你的帮助。

答案 1 :(得分:0)

在ubuntu上我使用MySQL并尝试使用终端编辑文件  sudo nano /etc/mysql/my.cnf
找到包含以下内容的行:
绑定地址
只需编辑此行
bind-address = 0.0.0.0
保存文件并用
重启mysql sudo服务mysql重启并重试

它对我有用,祝你好运!

答案 2 :(得分:0)

你错过了一些代码朋友!尝试在try catch块中添加#include <PN532.h> #include <SPI.h> /*Chip select pin can be connected to D10 or D9 which is hareware optional*/ /*if you the version of NFC Shield from SeeedStudio is v2.0.*/ #define PN532_CS 10 PN532 nfc(PN532_CS); #define NFC_DEMO_DEBUG 1 const int control = 7 ; const int speed = 200; void setup(void) { pinMode(control, OUTPUT) ;} void loop(void) { digitalWrite(control, HIGH); delay(2000); digitalWrite(control, LOW); delay(1000); } 。希望它有所帮助