Java Tutorial: How to connect a MySQL database and Java

In this tutorial, you will learn how to connect a MySQL database and java using IntelliJ IDEA. We use MySQL Connector/J driver for this tutorial. It’s an official JDBC driver for MySQL.

What is Java?

Java is a general-purpose, open-source and free programming language. It originally developed by Sun Microsystems in 1995 and more than 3 billion devices run Java. It is used to creating Desktop applications, Mobile applications, Web applications, Web servers, Games, and more. Java works on a variety of platforms. such as Windows, Mac, Linux and more. Java is easy to learn and simple to use.

Java Tutorial: Introduction for beginners (Basic)
Java Tutorial: Introduction for beginners – Part 2 (Basic)
Java Tutorial: Most Popular Java IDE in 2019

How to install IntelliJ IDEA on Windows (2019)

What is SQL?

SQL (Structured Query Language) is a standard language for storing, managing and retrieving data in databases. SQL was developed at IBM by Donald Chamberlin and Raymond Boyce. and first released in 1991. SQL became a standard of the ANSI (American National Standards Institute) in 1986.

SQL Tutorial: Introduction for beginners (Basic)

What is JDBC?

JDBC is an API for the Java language. It can be used to connecting to a database, sending queries, update statements to the database, retrieving and processing the results received from the DB.

Before starting this tutorial, you should have MySQL installed on your PC.

Installation (MySQL)

XAMPP – Apache (A) + MariaDB(M) + PHP(P) + Perl(P)

CtechF - How to install XAMPP - Screenshot

XAMPP is an Open source software. It’s a Stands of Cross-platform (X). we can create a local web server using XAMPP. It’s easy to use and it works with Windows, Linux, and Mac OS. You can use alternative software for XAMPP, such as WAMP, MAMP.

XAMPP Download link (121 MB) – https://www.apachefriends.org/index.html
WAMP download link (286 MB) – http://www.wampserver.com/en/#download-wrapper
MAMP download link (410 MB) – https://www.mamp.info/en/downloads/

How to install XAMPP.

  • Click on the downloaded XAMPP setup. (If you got any permission message or warning message click “OK” or “YES” button to continue) It will open the XAMPP setup wizard. Click the next button to continue.
CtechF - How to install XAMPP - Screenshot

Now it asked, what components we want to install and what’s not. We only need Apache and MySQL. (select following Components and click the next button to continue)

  • Server
    • Apache – Apache Server
    • MySQL – Database Server
  • Program Language
    • PHP – Hypertext Preprocessor
  • Program Languages
    • phpMyAdmin – Database Control panel
CtechF - How to install XAMPP - Screenshot
  • Choose installation location and click Next (Default: C:\xampp)  (If you got Windows Defender Firewall Permission request, Tick both checkboxes and click Allow access.)
  • Click the next button to continue Now it will install XAMPP (wait till the installation complete) After the installation complete. Click Finish.
  • It will open the XAMPP Control panel automatically.
  • This is a XAMPP Control Panel.
CtechF - How to install XAMPP - Screenshot
  • Now click Start Apache and MySQL (It will start Apache and MySQL servers)
CtechF - How to install XAMPP - Screenshot

Now visit the PHPMyAdmin (localhost/phpmyadmin) control panel or terminal. Then Create a New Database.

CREATE DATABASE db_java; /* Creating a Database */

/* Creating a Table */
USE db_java;
CREATE TABLE users (
        id INT NOT NULL AUTO_INCREMENT,
        fname VARCHAR(30) NOT NULL,
        lname VARCHAR(30),
        email VARCHAR(100) NOT NULL,
        contatno INT(10),
        note VARCHAR(400) NOT NULL,
        PRIMARY KEY (ID)
);

Database Name – db_java

Download MySQL Connector/J

First, go to the following page on the MySQL website. and download Platform Independent (Architecture Independent), ZIP Archive

{Select Operating System -> Platform Independents}
http://dev.mysql.com/downloads/connector/j

It will download a ZIP archive. Then Extract the downloaded file.

Create a New Project using the IntelliJ IDEA

Open IntelliJ IDEA and click Create New Project from the Welcome window. then click next without changing anything.

Now Change the project name and select a location to save the project file. Then click Finish

Project name: tutorial_java
Base Package: com.ctechf

Add MySQL Connector/J in IntelliJ IDEA

Start Coding – Connect a MySQL database and Java

First, create a Class called DBConnect and add the following code. Then save it.

DBConnect

//import class
import java.sql.*;

public class DBConnect {
    private Connection conn;
    private Statement stmt;
    private ResultSet rs;


    public DBConnect() {
        String url = "jdbc:mysql://localhost:3306/db_java"; //Database -> db_java
        String user = "root";
        String pass = "";

        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(url, user, pass);

            System.out.println("Connection Successful");
            conn.close();
        }catch (Exception ex){
            System.out.println("Error" + ex);
        }
    }
}

Then update Main class

public class Main {

    public static void main(String[] args) {
        DBConnect connect = new DBConnect();
    }
}

Output:

Hello Guys,

Hope this post (Java Tutorial: connect a MySQL database and java using IntelliJ IDEA) will help you to learn something, If you enjoy my work then please share my posts with your friends and anyone who might be interested in programming and don’t forget to subscribe my mailing list.

If you had any problem with this post, please do mention it in the comment section.

Like ctechf Facebook fan page: https://www.facebook.com/ctechf

Best Free eBooks to Learn Python Programming

Free eBooks to Learn Python, Here are 13 of the best Python eBooks for python lovers.
Download Now

What is Python?

Python is a scripting language and that is used for creating engineering-analysis tools, web development (server-side), software development, system scripting, and animation software. It designed by Guido van Rossum and first released in 1991. It’s an interpreted, High-level. General-purpose language. Python runs on Windows, Mac OS, and Linux.

Python Tutorial: Introduction for Beginners (Part 1)
Python Tutorial: Introduction for Beginners (Part 2)
Python Tutorial: How to Connect MySQL Database in Python
Python Tutorial: Python GUI Programming (Tkinter)
Python Tutorial: Send Emails Using Python
Python Tutorial: Django Tutorial for Beginners
Python Tutorial: Parsing command-line arguments

How to be a Programmer

Liked it? Take a second to support CtechF on Patreon!

2 thoughts on “Java Tutorial: How to connect a MySQL database and Java”

Leave a Reply

123 Shares
Share
Tweet
Share
Pin
Share