0

I am unable to connect mysql db from below mentioned example.

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();
    }
} }

The stacktrace is as follows

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
just_java
  • 11
  • 2
  • 1
    The stack trace will include the cause. Can you provide it? – samlewis Dec 16 '13 at 18:14
  • im getting the below error actually :( 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 – just_java Dec 16 '13 at 18:22
  • @just_java Consider adding that as an edit to the question. – SubSevn Dec 16 '13 at 18:26

1 Answers1

0

Is your mysql-connector jar file on your classpath? Also, how do you load your MySQL JDBC driver. It should be like the following:

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

} catch (Exception e) {
    e.printStackTrace();
}
blackpanther
  • 10,998
  • 11
  • 48
  • 78