How to Connect MySQL Database in Java Using Notepad

Hello Friends, Welcome to my new post. In this post, we will learn how to connect MySQL database in Java using notepad with step-by-step explanations. In my previous tutorial, we have already learned how to connect the MySQL database in Java using Eclipse IDE. In this post, we are not going to use any IDE instead of we are going to use Notepad to connect the Java program with the MySQL database.

So Let’s start our tutorial on how to connect MySQL database in Java using Notepad with the help of command prompt.

Also Read – 11 Best site to learn Java online For Free

How to Connect MySQL Database in Java Using Notepad

Downloading and Installing MySQL Command Line Client (Step 1)

  • The first step we need to take is downloading and installing the MySQL command-line client in our system.
  • If you don’t know how to correctly download and install MySQL command-line client in your system, then you can follow this guide.
  • After you have downloaded and installed it in your system, we can move ahead to the second step.

Creating a Database and Table in MySQL (Step 2)

  • Open MySQL command-line client, and enter your password so that you can connect to the MySQL database.
  • This is the same password you provided while installing the MySQL database in your system.
  • Now create a database named “demodb” and a table named “student” inside it with columns
    • ROLLNO
    • USERNAME
    • DEPT
how to connect mysql database in java using notepad
how to connect mysql database in java using notepad – fig -1

Creating Java Program in Notepad (Step 3)

  • Now in this step, we are going to create our Java Program.
  • Open Notepad.
  • You can also use Notepad++ for a better user experience.
  • Next, we have to import java.sql package to use the classes inside the java.sql package.
  • Now create a class and give a name to your class (JavaConnect in this example).
  • Now inside the class, we have to create our main method, i.e public static void main(String[] args).
  • Now save the file as JavaConnect.java
how to connect mysql database in java using notepad
how to connect mysql database in java using notepad – fig – 2
  • The program is given below.

Adding MySQL Connector jar File in Notepad (Step 4)

  • In order to connect your Java program with MySQL database, you need to include MySQL JDBC driver which is a JAR file, namely mysql-connector-java-8.0.27.jar. The version number in the Jar file can be different.
  • You can download the latest version of MySQL connector from this link (MySQL Connector Java download).
  • After visiting the link, select platform independent from the drop-down menu as shown in the figure below.
how to connect mysql database in java using notepad
how to connect mysql database in java using notepad – fig -3
  • Then download the Platform Independent (Architecture Independent), ZIP Archive as shown below.
how to connect mysql database in java using notepad – fig – 4
  • Extract the zip archive and you will get the jar file.
  • Next, you have to include this jar file into your program so that you will be able to connect your Java program with MySQL database.
  • To do this, navigate to the location where you have saved the Java program file in your system and then paste the extracted jar file.

Connecting Java Program with MySQL Database (Step 5)

Since you have already finished adding jar file, now you are ready to connect your Java program with MySQL Database. Let’s quickly take a look at the steps.

  • Establish a connection using DriverManager.getConnection(String URL) and it returns a Connection reference.
  • In String URL parameter you have to write like this  jdbc:mysql://localhost:3306/demodb”, “root”, “root”  where,
    • jdbc is the API.
    • mysql is the database.
    • localhost is the name of the server in which MySQL is running.
    • 3306 is the port number.
    • demodb is the database name. If your database name is different, then you have to replace this name with your database name.
    • The first root is the username of the MySQL database. It is the default username for the MySQL database.
    • The second root is the password that you give while installing the MySQL database.
  • SQL Exception might occur while connecting to the database, So you need to surround it with the try-catch block.
  • Programming Example is given below.

Running Java Program in Command Prompt (Step 6)

  • Now open the command prompt and navigate to the folder where you have saved the Java program file.
  • Next, compile the Java file into Java class file using the javac command as shown in the figure below.
how to connect mysql database in java using notepad – fig – 5

 

  • To execute the program, we have to give the command like this.

java -cp .;path of your mysql-connector.jar className

  • Where cp command is being used to add the MySQL connector jar file while running the program.
  • You can see the figure below.
how to connect mysql database in java using notepad – fig – 6
  • As we can see that we have successfully connected the MySQL database with our Java program using Notepad.

So guys now  I am wrapping up this post about How to Connect MySQL Database in Java Using Notepad and Feel free to drop us a comment if you find out something difficult to understand. And to get these types of awesome posts stay tuned with Tutorials Field. HAPPY CODING!!!

Some Best Java Tutorials On Tutorials Field…

Leave a Comment