Tuesday, 9 August 2016

MySQL Connection in Eclipse(Install, Connect and Test)

Hello everyone!

I have started a new project in which I am planing to use JSF, Primefaces and DB features. But this time I want to use MySQL instead of Oracle 11g in my project. I posted some entries about Oracle 11g while I was learning about it during the development of previous Java EE projects. I believe switching to and tackling MySQL will do huge benefits. Hope you can find what you have been looking for in this post too!


Let's go step by step as usual :)

STEP 1 - Make Sure You Have The Essentials

Please make sure you downloaded and configured all the necessary things with Eclipse and JDK. If you haven't and need help with this, check this post out and have a little Google Search please!(As I don't have a tutorial on how to install Eclipse on my blog yet :D sorry guys! )

STEP 2 - Download MySQL Installer and Install It

Time to download MySQL Installer. You can get it from here. This page will give you necessary information. It is also possible to download just the MySQL Server and configure it. But I found MySQL Installer more helpful. It has all the components that you may need. Easy to install and easy to use!

As you can see above, there are only 32-bit versions listed but it says on the page that 32-bit version will install both 32-bit and 64-bit binaries. So don't worry, we are good to go :) Download the one you choose(one requires Internet connection during installation process, one doesn't) and install it. It is simply a next-next process.
 In this step, you can specify root password and create a new user.
After the installation is completed, it will ask you if you want to run MySQL Workbench. Say yes and follow the next step.

STEP 3 - Workbench Fun(New Schema)

You will see the welcome window as below. As you can see there is the "root" and the other user("selen") that we configured in the installation process.
I am going to connect as "root" and create a new schema "estateagent" for my new project. 
P.S. You need to make sure that MySQL is started before you try connect to it on MySQL Workbench. Follow these steps if you do not know how.
1)Click "Start" on your computer and search for "Services"
2)Click on "Services" to reach the list and find "MySQL57"(or so).
3)Right click and start the service. Then you can use MySQL Workbench safe and sound :)

STEP 4 - Download MySQL Connector and Add It To Your Project

Now it is time to head over to Eclipse and create a new dynamic project. Then we will download MySQL connector and add it to our project. You can download MySQL connector from here. As you can see below there are two download options, I downloaded .zip file.

After download, unzip the folder and move the jar file whereever you fancy on your computer. I moved it under my D:// drive.
Now go ahead to Eclipse, find your freshly created project and right-click on the project. Follow "Build Path->Configure Build Path". Then you will see a window like below. Simply add the .jar file into your project by following the steps shown in the image below. When it's done you will see this .jar file in your project structure under "Referenced Libraries".

STEP 5 - Test If Everything Is OK

Lastly, we will write a simple java class to test if we can connect to "estateagent" schema from our project. You can see the code below.
To run this, right click on your java class and follow "Run As->Java Application". If everything is ok, you will see the success message in the console when compiled.
 Hope to see you in the next blog post!

Selen

No comments:

Post a Comment