For connecting java application with the database, you need to follow 7 steps to perform database connectivity.

In this example we are using MySql as the database. So we need to know following information for the mysql database:

  1. Download mysql-connector.jar and add in your IDE.
  2. Driver class: The driver class for the mysql database is com.mysql.jdbc.Driver.
  3. Create a Connection object:

URL:  The connection URL for the mysql database is jdbc:mysql://localhost:3306/mydb where jdbc is the API, mysql is the      database, localhost is the server name on which mysql is running, we may also use IP address, 3306 is the port number and mydb is the database name.

Username: The default username for the mysql database is root.

Password: Password is given by the user at the time of installing the mysql database.

  1. Create a Statement object
  2. Execute query
  3. Process ResultSet
  4. Close the connection


import java.sql.*;  

class MysqlCon{  

      public static void main(String args[]){  



                 Connection con=DriverManager.getConnection(  


                  Statement stmt=con.createStatement();  

                  ResultSet rs=stmt.executeQuery("select * from emp");  

                 while(rs.next()) { 

                    System.out.println(rs.getInt(1)+"  "+rs.getString(2));  



                }catch(Exception e){ System.out.println(e);}