2013-12-16 82 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上设置类路径.. –

相关问题