It is very simple to establish a connection to the Database from a Java program
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://serveripaddress:portnumber";
Connection con = DriverManager.getConnection(url,"username","password");
In first line, Class.forName() method is mainly used to register the Database Driver.
The second line specifies the URL for establishing connection to the database, which you want to connect.
URL stands for "Uniform Resource Locator". You will be familiar with HTTP URL's that you normally use to access a web site (Eg: http://www.websitename.com). URLs are used to identify a resource using a unique name.Similarly JDBC also uses URL to connect to the database.
In HTTP, you will begin a URL with the protocol name i.e. http: . Similarly, the JDBC driver URL also should start with the protocol name jdbc: . Next, the subprotocol represents the database you want to connect (eg: mysql, oracle, odbc, etc.). While subname provides additional information on how and where to connect.
jdbc:subprotocol:subname