MySQL服务器 - JDBC连接

时间:2016-01-22 03:05:03

标签: java mysql jdbc

我在使用ConnectJ驱动程序连接到MySQL服务器时遇到问题。 我写了一个简单的程序,只是为了建立连接,如下所示:

private static final String DB_DRIVER = "com.mysql.jdbc.Driver";
private static final String DB_CONNECTION = "jdbc:mysql://[host]:1521/OBIEE11G";    
private static final String DB_USER = "OBADW";
private static final String DB_PASSWORD = "OBADW";

public static Connection getConnection() {

    Connection dbConnection = null;
    try {
        // DriverManager.registerDriver(new com.mysql.jdbc.Driver());
        Class.forName(DB_DRIVER);
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
        System.out.println(e.getMessage());
    }
    try {
        // DriverManager.registerDriver(new com.mysql.jdbc.Driver());
        System.out.println("before get connection ***********");
        System.out.println("Java version: " + System.getProperty("java.version"));
        Driver driver = (Driver) Class.forName("com.mysql.jdbc.Driver").newInstance();
        System.out.println("JDBC driver: " + driver.getMajorVersion() + "." + driver.getMinorVersion());
        dbConnection = DriverManager.getConnection(DB_CONNECTION, DB_USER, DB_PASSWORD);
        System.out.println("Connection: " + dbConnection.getClass().getName());
        System.out.println("after get connection ***********");
        return dbConnection;
    } catch (SQLException | InstantiationException | IllegalAccessException | ClassNotFoundException e) {
        e.printStackTrace();
        System.out.println(e.getMessage());
    }
    return dbConnection;
}

public static void main(String[] args) {
    Connection dbConnection = getConnection();

    if (dbConnection != null) {
        System.out.println("Connected");
    } else {
        System.out.println("Failed");
    }

我收到以下错误:

before get connection ***********
Java version: 1.7.0
JDBC driver: 5.1
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:85)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:57)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:541)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:404)
    at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:983)
    at com.mysql.jdbc.MysqlIO.readPacket(MysqlIO.java:628)
    at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1014)
    at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2254)
    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2285)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2084)
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:795)
    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:44)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:85)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:57)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:541)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:404)
    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:400)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:327)
    at java.sql.DriverManager.getConnection(DriverManager.java:582)
    at java.sql.DriverManager.getConnection(DriverManager.java:226)
    at JDBCConnection.getConnection(JDBCConnection.java:32)
    at JDBCConnection.main(JDBCConnection.java:45)
Caused by: java.io.EOFException: Can not read response from server. Expected to read 4 bytes, read 0 bytes before connection was unexpectedly lost.
    at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:2949)
    at com.mysql.jdbc.MysqlIO.readPacket(MysqlIO.java:560)
    ... 17 more
Communications link failure

成功发送到服务器的最后一个数据包是0毫秒前。驱动程序未收到来自服务器的任何数据包。 失败

我正在使用connectJ:mysql-connector-java-5.1.37-bin.jar Java 1.7,JDBC驱动程序5.1

我能够ping主机,并使用mysql开发人员连接它。 请建议。

2 个答案:

答案 0 :(得分:1)

从您的日志中明确了解“通信链接失败”

  • 确保您的mysql服务器正在运行
  • 确保您拥有正确的主机名/ ip / port
  • 确保您的网络正常运行

答案 1 :(得分:1)

听起来很愚蠢,但您是否已在项目属性中正确配置了MySQL Connector.jar文件?