Java JDBC How to - Connect to a remote mysql database








Question

We would like to know how to connect to a remote mysql database.

Answer

import java.sql.Connection;
import java.sql.DriverManager;
//from   ww  w .j  a va2 s.c  om
public class Main {
  public static void main(String[] args) throws Exception {
    String url = "jdbc:mysql://localhost:3306/mydb";
    Class.forName("com.mysql.jdbc.Driver");
    Connection conn = DriverManager.getConnection(url, "root", " ");
    System.out.println("Database connection established");
    conn.close();
    System.out.println("Database connection terminated");
  }
}