Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them, it only takes a minute:

So I've been teaching myselft mysql and am trying to integrate it into my Java code. I've looked at some past code snippets and tutorials and I can't seem to figure out why my code is incorrect. (removed password for obvious reasons)

Here's what I'm using to connect

  public static void connectionToMySql(){
    String host = "mysql9.000webhost.com";
    String username = "a9808220_pin";
    String pass = "";
    try{
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        Connection connection = DriverManager.getConnection(host,username,pass);
      /*insert code*/
        connection.close();
        System.out.println("It worked :)");

    } catch (Exception e) {
        System.out.println("Something went wrong :(");
        e.printStackTrace();
    }
}

I get an exception that there's no suitable driver. I'm not sure why because I have the jar downloaded and pathed correctly

share|improve this question
1  
What is the exact type of the Exception you get? And how you included the MySQL jar in your classpath? – marczeeee Jan 13 at 23:16
    
"java.sql.SQLException: No suitable driver found for mysql9.000webhost.com" I downloaded the jar and set my project structure so that it used it. Before I had it, it said it couldn't find the driver class. But after I downloaded the jar, it now gets this exception. – ComputerDope Jan 13 at 23:17
    
In row 6 you just instantiating the MySQL driver but doing nothing with it, not even assigning it to a variable. What if you register the created Driver object instance with DriverManager.registerDriver()? – marczeeee Jan 13 at 23:20
    
Like this? " Driver driver = (Driver) Class.forName("com.mysql.jdbc.Driver").newInstance(); DriverManager.registerDriver(driver);" still get the same thing – ComputerDope Jan 13 at 23:23
    
I would be really surprised if that's the problem because that's how I've seen it done in many tutorials and snippets – ComputerDope Jan 13 at 23:23

2 Answers 2

String host should be like

String host = "jdbc:mysql:ip:port"//ip or hostname

example code that works for me...

        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:1433;"
                 +"databaseName=test;"
                 +"user=sa;"
                 +"password=xxxxx;");
         System.out.println("connected");
share|improve this answer

Your connection should be a JDBC URL in the form

String url = "jdbc:mysql://mysql9.000webhost.com:3306/";

Now in order for this to work you should:

1) Verify that mySQL is running on the host mysql9.000webhost.com

2) Verify that the port is the default port ie 3306, if not change the code above to use the right port

share|improve this answer
    
Thanks, I got past that exception. But for some reason now I timeout trying to connect and I'm unsure why it can't connect successfully – ComputerDope Jan 13 at 23:43
    
You need debug the connectivity. It may be any number of things: server is unreachable, its not running on the default port, etc.. – xtrakBandit Jan 14 at 19:41

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.