Font Awesome Icons

Tutorial

Connectivity with MySQL

For connecting java application with the mysql database, you need to follow 5 steps to perform database connectivity:

1)Driver class: The driver class for the mysql database is com.mysql.jdbc.Driver.

2)Connection URL: The connection URL for the mysql database is jdbc:mysql://localhost:3306/alok 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 alok is the database name.

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

4)Password: Password is given by the user at the time of installing the mysql database. In this example, we are going to use root as the password.

Example to Connect Java Application with mysql database:

import java.sql.*; 

class MysqlCon

public static void main(String args[])

Try

Class.forName(“com.mysql.jdbc.Driver”); 

 

Connection con=DriverManager.getConnection( 

“jdbc:mysql://localhost:3306/alok”,”root”,”root”); 

 

//here alok is database name, root is username and password

 Statement stmt=con.createStatement(); 

 

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

 

while(rs.next()) 

System.out.println(rs.getInt(1)+”  “+rs.getString(2)+”  “+rs.getString(3)); 

 

con.close(); 

 

}

catch(Exception e)

{

System.out.println(e);

  } 

}

Hi, Welcome to etechvidya