Ubuntu上的MySQL异常:com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:通信链接失败

时间:2013-12-16 18:07:11

标签: java mysql jdbc

我无法从下面提到的示例中连接mysql db。

  

class 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 个答案:

答案 0 :(得分:0)

你的类路径上有mysql-connector 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();
}