How to Connect MySQL Database in Java Using Eclipse Step by Step

Hello guys, Welcome to another tutorial, and in this tutorial, we will learn how to connect MySQL database in Java using Eclipse step by step. Most of the time, whenever we try to connect our program to the database, we face a lot of problems, and even then, we cannot connect successfully. But if you follow this tutorial properly, you will be able to connect your Java program to the MySQL database using Eclipse quite easily.

So Let’s start our tutorial on How to Connect MySQL Database in Java using Eclipse or can say JDBC Connection in Eclipse with MySQL Database.

Also Read – How to Connect MySQL Database in Java Using NetBeans

How to Connect MySQL Database in Java Using Eclipse

Downloading and Installing Eclipse

  • The very First thing you need to do is to Download and Install Eclipse IDE on your system.
  • Open your favorite browser and go to the official website of Eclipse www.eclipse.org.
  • When you enter the official website of the eclipse, you will see a download button in the top right corner. Click on that.
How to connect mysql database in java using eclipse
MySQL with Java in Eclipse – fig -1
  • After clicking on the Download Button, you will be redirected to another Web page. Click on the download button, as shown in the figure below.
How to connect mysql database in java using eclipse
MySQL with Java in Eclipse – fig -2
  • Now again, click on the Download button to download the executable eclipse file.
How to connect mysql database with java using eclipse
MySQL with Java in Eclipse – fig -3
  • After downloading the file, you need to install it in your system in the same way you install any application.
  • In the next steps, we will see how to connect Eclipse to MySQL Database in Java.

Creating a Database and Table in MySQL

  • To create a database and table, you can either choose MySQL command-line client or MySQL workbench, which is nothing but a GUI version of the MySQL console where we can visually design, model, and generates databases.
  • If you don’t know how to install MySQL workbench in your system, then you can follow this guide, How to install MySQL Workbench.
  • To get to know how to download and Install MySQL in your system so that you can Use MySQL command-line client, you can follow this guide.
  • I will use MySQL command-line client in this tutorial, open MySQL command-line client, and enter your password so that you can connect 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
MySQL with Java in Eclipse – fig -4

Creating a Java Project in Eclipse

  • Open Eclipse IDE and click on the Java Project under the new section of File Menu (File>>New>>Java Project).
MySQL with Java in Eclipse – fig -5
  • Now give a name to your project (DbConnect in this example) and click on “Finish”.
MySQL with Java in Eclipse – fig -6
  • Now right click on the project and create a new Java class (New>>Class).
MySQL with Java in Eclipse – fig -7
  • Now give a name to your class(MyDbconnect in this Example), tick mark on the public static void main(String[] args), and then click on the Finish button as shown in the figure below.
MySQL with Java in Eclipse – fig -8

Adding MySQL connector jar file in Eclipse

  • 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.28.jar. The version number in the Jar file can be different. So in this step, we will see how to add MySQL connector jar file in Eclipse.
  • 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 Eclipse – fig – 9
  • Then download the Platform Independent (Architecture Independent), ZIP Archive as shown below.
Connect MySQL database in Java Using Eclipse – fig – 10
  • 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.
  • Right-click on the project, go to the properties section then select Java Build Path and click on the Add External JARs button.
How to connect mysql database in java using eclipse
MySQL with Java in Eclipse – fig -10
  • After clicking on the button a pop-up window will appear where you have to select and open the jar file.
How to connect mysql database in java using eclipse
How to connect mysql database in java using eclipse-fig-11
  • You can see the added Jar file as shown in the figure below. Now click on the Apply and Close button.
How to connect mysql database in java using eclipse
How to connect mysql database in java using eclipse-fig-12

Connecting Java Program with MySQL Database | JDBC Connection

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 Java code to connect to MySQL database in Eclipse step by step.

  • 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.

Inserting Data into The MySQL Database using Java

As we saw right away how to connect MySQL database in Java Using Eclipse, now its time to insert some data into our table. If you are familiar with the Prepared Statement in Java and about SQL INSERT query, then it will be a lot of easier for you to add data to your table.

  • Create a PreparedStatement object and use SQL INSERT query to add data to the table.
  • Specify the values for each parameter through the PreparedStatement object.
  • Execute the query through the PreparedStatement object.
  • Now run your program.
  • To check whether data is inserted in the table or not, open MySQL database and connect it by entering the password and write the following query.
How to connect mysql database in java using eclipse
How to connect mysql database in java using eclipse-fig-13

Updating Data in The MySQL Database using Java

  • To update data in the table, you have to use SQL UPDATE query.
  • Now run your program.
  • To check whether data is updated in the table or not, open MySQL database and connect it by entering the password and write the following query.
How to connect mysql database in java using eclipse-fig-14

How to Retrieve Data from MySQL Database in Java Using Eclipse

  • To retrieve data from the table, we will use SQL SELECT query.
  • In order to get the results, we will use Java ResultSet object.

Deleting Data from the MySQL Database using Java

  • To delete data from the table, we will use SQL DELETE query.

Conclusion

Finally, We have concluded that to Connect JDBC to MySQL in Eclipse is not as difficult as we think. By following the simple steps that I have taught in this tutorial, you can effortlessly connect your Java Projects with the MySQL database.

So, guys, I am wrapping up this tutorial “How to connect MySQL Database in Java Using Eclipse” and Feel free to drop us a comment if you find out something difficult to understand.

People Are Also Reading…

6 thoughts on “How to Connect MySQL Database in Java Using Eclipse Step by Step”

  1. Hi! Thank you for this explanation. You explained it very simply and I could understand.
    However, I’m having a problem that you didn’t mention here…

    When I go to “Database Connections” in Eclipse, I try to establish a “new” connection profile, there are no visible drivers to choose from the drop-down menu. I have watched so may tutorials but I have never seen this problem. There should be some MySQL drivers visible, but I don’t see anything. My teachers thinks it’s an issue within my computer’s system settings.
    I am a new Java student and I cannot progress with my classes until I finish this step. I would be so thankful if you could help me. Thanks

    Reply
    • Hello Sir,
      Thank you for your valuable comment.

      You can contact me to the whatsapp number +917549123182 so that you can send me the screenshot of your problem.

      Thanks and Regrads

      Reply
  2. Appreciating very much for your tutorial. Had problems with interactions and your article helped us a lot.
    Best regards,

    Reply

Leave a Comment