2016-01-22 249 views
1

我在使用ConnectJ JDBC驅動程序連接到mySQL服務器時遇到問題。 我寫了一個簡單的程序,只是爲了建立如下連接:MySQL服務器 - JDBC連接

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的開發人員連接。 請建議。

+0

是的,它在dbConnection = DriverManager.getConnection(DB_CONNECTION,DB_USER,DB_PASSWORD)上失敗;線,我也ping服務器,並且它似乎已啓動並運行 – sagabh

+0

可能的重複[com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:通信鏈接失敗](http://stackoverflow.com/questions/2983248/ com-mysql-jdbc-exceptions-jdbc4-communicationsexception -communication-link-fai) –

+0

是的,我經歷了這篇文章,但是,我能夠使用我的本地mysql開發人員連接到主機。它看起來不像後 – sagabh

回答

1

從你的日誌其對「通信鏈路故障」

  • 確保你的MySQL服務器運行
  • 確保您有正確的主機名/ IP /端口
  • 確保您的網絡明確工作
+0

是的,我能夠使用sql開發人員連接到主機,並且我試圖ping主機 – sagabh

+0

Ping使用的協議不同於您的Java代碼將用於與其通信的協議,所以只是因爲ping工作並不意味着JDBC會。 –

+0

那麼如何測試jdbc連接? – sagabh

1

聽起來很愚蠢,但你有沒有在你的項目屬性中正確配置你的MySQL Connector.jar文件?