0

i want to open a MySql connection from java. and this my code :

public static void main(String [] args){
    String line;
    Connection connection;
    Statement statement;
    Class.forName("com.mysql.jdbc.Driver").newInstance();
        connection=DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "manager");
        statement =connection.createStatement();

        System.out.println("Database Created...!!!");
        System.out.println("Connected");


}

but it give me this error:

    com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

    Last packet sent to the server was 1 ms ago.
        ...
Caused by: java.net.ConnectException: Connection timed out: connect

why? and what it should be do? please help me. thank you...

1

2 Answers 2

1

This is because your mysql might not be running. Try to telnet by telnet 127.0.0.1 3306 If you get error then mysql is not running for sure. you can also check

netstat -aon | find /i "listening"

this will show all listening ports

Sign up to request clarification or add additional context in comments.

2 Comments

the mysql server is running successfully but the same error it appear in java
@ooo I think you are using windows 7 and in this OS telnet is by default disabled.You need to enable it first
0

It is the connection problem between the driver and the server. Please ensure that the driver version and the server version match. For example, if your server version is MySQL 4.0 or 3.23, you should use mysql-connector-java-3.0.11-stable-bin.jar instead of the latest version.

5 Comments

my server version is " 5.1.50-community " witch mysql connector should be download/?
Refering link Table 20.22. Summary of Connector/J Versions Connector/J version Driver Type JDBC version MySQL Server version Status 5.1 4 3.0, 4.0 4.1, 5.0, 5.1, 5.5, 5.6, 5.7 Recommended version 5.0 4 3.0 4.1, 5.0 Released version 3.1 4 3.0 4.1, 5.0 Obsolete 3.0 4 3.0 3.x, 4.1 Obsolete
1. Could you utilize the default mysql client (i.e. mysql) to access the local database "test" by the same username and the password? 2. Could you try to stop the local firewall if possible?
i'm developing a java web application in which it should be desplay the my sql open session,so i want access on mysql to show the open sessions
Could you try to do the two steps I suggested? as I suspected that test was not accessible.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.