Java MySQL JDBC Driver class and URL example

Java MySQL Driver FAQ: Can you share a Java/MySQL JDBC Driver and URL example, i.e., how to connect to MySQL in Java?

Sure. Here's a quick post to help anyone that needs a quick MySQL JDBC Driver and URL reference.

The basic MySQL JDBC Driver and Java MySQL URL information you need is shown here:

// MySQL URL (JDBC Connection) String:
jdbc:mysql://HOST/DATABASE

// MySQL JDBC Driver String:
com.mysql.jdbc.Driver

MySQL JDBC Driver and URL connection example

If it helps to see the Driver and URL connection strings in a sample application, here's a little Java example that shows how to use the MySQL JDBC Driver and URL to establish a database connection:

public class JdbcMySQLDriverUrlExample
{
  public static void main(String[] args)
  {
    Connection connection = null;
    try
    {
      // the mysql driver string
      Class.forName("org.mysql.Driver");
    
      // the mysql url
      String url = "jdbc:mysql://THE_HOST/THE_DATABASE";
      
      // get the mysql database connection
      connection = DriverManager.getConnection(url,"THE_USER", "THE_PASSWORD");
      
      // now do whatever you want to do with the connection
      // ...
      
    }
    catch (ClassNotFoundException e)
    {
      e.printStackTrace();
      System.exit(1);
    }
    catch (SQLException e)
    {
      e.printStackTrace();
      System.exit(2);
    }
  }
}

If you need to use MySQL with Java, I hope this example has been helpful.

Share it!

There’s just one person behind this website; if this article was helpful (or interesting), I’d appreciate it if you’d share it. Thanks, Al.

Add new comment

The content of this field is kept private and will not be shown publicly.

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.