2013-12-16 85 views
0

我無法連接下面提到的示例中的mysql db。Ubuntu上的MySQL異常:com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:通信鏈接失敗

類JDBCTest {

private static final String url = "jdbc:mysql://localhost"; 

private static final String user = "root"; 

private static final String password = "abc"; 

public static void main(String args[]) { 
    try { 
     Connection con = DriverManager.getConnection(url, user, password); 
     System.out.println("Success"); 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} } 

堆棧跟蹤如下:

Caused by: org.springframework.jdbc.CannotGetJdbcConnectionException: 
Could not get JDBC Connection; nested exception is 
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure 
property file provide to spring contains: jdbcDriverClassName=com.mysql.jdbc.Driver 
jdbcUrl=jdbc:mysql://localhost:3306/mynumber jdbcUsername=root jdbcPassword=rock 
+1

堆棧跟蹤將包含原因。你能提供嗎? – samlewis

+0

即時獲取下面的錯誤:( )引起:org.springframework.jdbc.CannotGetJdbcConnectionException:無法獲取JDBC連接;嵌套異常是com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:通信鏈接失敗 屬性文件提供到彈簧包含: jdbcDriverClassName = com.mysql.jdbc.Driver JDBCURL = JDBC:MySQL的://本地主機:3306/mynumber的 jdbcUsername =根 jdbcPassword =岩石 –

+0

@just_java考慮添加,作爲一個編輯的問題 – SubSevn

回答

0

是在classpath你的MySQL連接器jar文件?另外,你如何加載你的MySQL JDBC驅動程序。它應該如下所示:

try { 
    Class.forName("com.mysql.jdbc.Driver"); 
    Connection con = DriverManager.getConnection(url, user, password); 
    System.out.println("Success"); 

} catch (Exception e) { 
    e.printStackTrace(); 
} 
+0

。我正在使用Ubuntu,並已在ubuntu上設置類路徑.. –

相關問題