Connecting to a MySQL Database in Java

In java we have been provided with some classes and APIs with which we can make use of the database as we like. Database plays as very important role in the programming because we have to store the values somewhere in the back- end.

Connecting to a MySQL Database in Java

Connecting to a MySQL Database in Java

     

In java we have been provided with some classes and APIs with which we can make use of the database as we like. Database plays as very important role in the programming because we have to store the values somewhere in the back- end. So, we should know how we can manipulate the data in the database with the help of java,  instead of going to database for a manipulation. We have many database provided like Oracle, MySQL etc. We are using MySQL for developing this application. 

In this section, you will learn how to connect the MySQL database with the Java file. Firstly, we need to establish a connection between MySQL and Java files with the help of MySQL driver .  Now we will make our  account in MySQL database so that we can get connected to the database. After establishing a connection  we can access or retrieve data form MySQL database. We are going to make a program on connecting to a MySQL database, after going through this program you will be able to establish a connection on your own PC.

Description of program:

This program establishes the connection between MySQL database and java files with the help of various types of APIs interfaces and methods. If connection is established then it shows "Connected to the database" otherwise it will displays a message "Disconnected from database".

Advertisement

Description of code:

Connection:
This is an interface in  java.sql package that specifies connection with specific database like: MySQL, Ms-Access, Oracle etc and java files. The SQL statements are executed within the context of the Connection interface.

Class.forName(String driver):
This method is static. It attempts to load the class and returns class instance and takes string type value (driver) after that matches class with given string.

DriverManager:
It is a class of java.sql package that controls a set of JDBC drivers. Each driver has to be register with this class.

getConnection(String url, String userName, String password):
This method establishes a connection to specified database url. It takes three string types of arguments like: 

    url: - Database url where stored or created your database
  userName: - User name of MySQL
  password: -Password of MySQL 

con.close():
This method is used for disconnecting the connection. It frees all the resources occupied by the database.

printStackTrace():
The method is used to show error messages. If the connection is not established then exception is thrown and print the message.

Here is the code of program:

import java.sql.*;

public class MysqlConnect{
  public static void main(String[] args) {
  System.out.println("MySQL Connect Example.");
  Connection conn = null;
  String url = "jdbc:mysql://localhost:3306/";
  String dbName = "jdbctutorial";
  String driver = "com.mysql.jdbc.Driver";
  String userName = "root"
  String password = "root";
  try {
  Class.forName(driver).newInstance();
  conn = DriverManager.getConnection(url+dbName,userName,password);
  System.out.println("Connected to the database");
  conn.close();
  System.out.println("Disconnected from database");
  catch (Exception e) {
  e.printStackTrace();
  }
  }
}

Download this example.

Tutorials

  1. Mapping MySQL Data Types in Java
  2. Connecting to a MySQL Database in Java
  3. Creating a Database in MySQL
  4. Creating a Database Table
  5. Creating a MySQL Database Table to store Java Types
  6. Description of Database Table
  7. Deleting a Table from Database
  8. Retrieving Tables from a Database
  9. Inserting values in MySQL database table
  10. Retrieving All Rows from a Database Table
  11. Adding a New Column Name in Database Table
  12. Change Column Name of a Table
  13. Make Unique Column in Database Table
  14. Remove Unique Column in Database Table
  15. Arrange a Column of Database Table
  16. Arrange a Column of Database Table
  17. Sum of Column in a Database Table
  18. Deleting All Rows from a Database Table
  19. Delete a Specific Row from a Database Table
  20. Delete a Column from a Database Table
  21. Join tables in the specific database
  22. Join tables with the NATURAL LEFT JOIN operation
  23. Join tables with the NATURAL RIGHT JOIN operation
  24. Cross Join Tables in a Specific Database
  25. Prepared Statement Set Object
  26. Statement Batch Update
  27. Prepared Statement With Batch Update
  28. Select Records Using Prepared Statement
  29. Update Records using Prepared Statement
  30. Inserting Records using the Prepared Statement
  31. Count Records using the Prepared Statement
  32. Deleting Records using the Prepared Statement
  33. Using the Prepared Statement Twice
  34. Set Data Types by using Prepared Statement
  35. Set byte, short and long data types by using the Prepared Statement
  36. Prepared Statement Set Big Decimal
  37. Set Date by using the Prepared Statement
  38. Set Time by using the Prepared Statement
  39. Set Timestamp by using the Prepared Statement
  40. Copy Table in a MySQL Database