0
votes

I'm trying to connect to a MySQL database using the program below. Depending on how I present the connection string I get the following errors.

IP address separated from 'mysql' by ':' only: conn = DriverManager.getConnection("jdbc:mysql:[valid ip address]/localhost:3306/[valid database name]","[valid username]","[valid password]");

SQLState: 08001 VendorError: 0 java.sql.SQLException: No suitable driver found for jdbc:mysql:[valid ip address]/localhost:3306/[valid database name]

IP address separated from 'mysql' by ':http://': conn = DriverManager.getConnection("jdbc:mysql:http://[valid ip address]/localhost:3306/[valid database name]","[valid username]","[valid password]");

SQLState: 08001 VendorError: 0 java.sql.SQLException: No suitable driver found for jdbc:mysql:http://[valid ip address]/localhost:3306/[valid database name]

IP address separated from 'mysql' by '://': conn = DriverManager.getConnection("jdbc:mysql://[valid ip address]/localhost:3306/[valid database name]","[valid username]","[valid password]");

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server. SQLState: 08S01 VendorError: 0

In the latter instance a stacktrace provides 'Caused by: java.net.ConnectException: Connection refused: connect'

In all cases the stacktrace also indicates that the line of code setting the variable 'conn' is throwing the exception.

Note that in the program listing text within square brackets represents obfuscated code and that the brackets are not actually in the program.

My question is, do I actually have the required drivers installed or not, or is there a problem with my connection string? Could there be some other issue that I haven't thought of?

Code follows

package [package name];

import org.junit.Test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class SQLConnectionTest
{

    @Test
    public void startWebDriver() 
    {
        try 
        {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
        } 
        catch (Exception ex) 
        {
            System.out.println(ex.getMessage());
        }

        Statement stmt = null;
        Connection conn = null;
        ResultSet rs = null;

        try 
        {
            conn = DriverManager.getConnection("jdbc:mysql://[valid ip address]/localhost:3306/[valid database name]","[valid username]","[valid password]");

            // Do something with the Connection

        } 
        catch (SQLException ex) 
        {
            // handle any errors
            System.out.println("SQLState: " + ex.getSQLState());
            System.out.println("VendorError: " + ex.getErrorCode());
            ex.printStackTrace();
        }

        try 
        {
            conn.close();
        } 
        catch (SQLException e) 
        {
            System.out.println(e.getMessage());
        }
    }
}

Some information on the environment:

IDE: Eclipse Luna Service Release 2 (4.4.2), Build id: 20150219-0600

jre System Library: jre1.8.0_45

MySQLConnector library: mysql-connector-java-5.1.36.jar (appears in Referenced Libraries section of Package Explorer)

I've also installed mysql-connector-java-gpl-5.1.36, jdk-8u45-windows-x64 and selenium-java-2.45.0

I'll be happy to provide any further information as required.

1

1 Answers

0
votes

The connection process seems OK to me, however there's something strange in the connection URL. You have

conn = DriverManager.getConnection("jdbc:mysql://[valid ip address]/localhost:3306/[valid database name]","[valid username]","[valid password]");

which has IP address and localhost:port. This is not a valid URL, yo have to remove either the localhost or the IP address:

conn = DriverManager.getConnection("jdbc:mysql://[valid ip address]:3306/[valid database name]","[valid username]","[valid password]");

or

conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/[valid database name]","[valid username]","[valid password]");

That should do the trick.