This tutorial is about Connecting Java to MySQL database using JDBC driver. You may find many tutorial on this topic but most of them show you the code and links to download driver and install it which is what i am also gonna do but in easy, straight forward way.
You need MySQL database before proceeding. Installing XAMPP in your system helps a lot because apart of MySQL you will get many other handy tools coupled as phpmyadmin package. So if you do not have it, please install it and run it. You should have this screen in XAMPP control panel.
Since this tutorial is about connecting to MySQL database, i am not going to create new database or that stuff, instead i am going to use databases which come’s as example in MySQL database. To know it go to http://localhost:1000/phpmyadmin/. I am going to use “cdcol” database (it is example database comes with installation) to demonstrate my code.
NOTE: For this tutorial and running code successfully i am going to ask you to download latest Java from here and Eclipse editor. If you running Windows 64 bit like me then choose 64 bit Java and 64 bit Eclipse else you may face any issue (issue is debuged already. See here).
Open Eclipse, Create new Java project from File Menu. Inside “src” folder in Project on “Package Explorer” create new Java file and name it properly.
Now it’s time to add JDBC library in our project. Right click on Project in “Package Explorer” and Build path -> Configure Build Path. Window Pops up. Click on “Add External Jar” and choose the Jar file from Computer.
Link: Download JDBC Jar file.
Ok now you have done with the configuration of JDBC driver. It’s time to code our project and check whether it’s working or not.
Import these classes which is Java built in class for SQL operation. To load the JDBC driver we are going to use “Class.forName“. This line will load it.
This line will load the JDBC. Now its time to create Connection variable in order to connect to our database. So let’s do it. We are going to use “getConnection()” method. Syntax for it as shown below.
DriverManager.getConnection(“jdbc:mysql://<Host>/Database”,”<MySQL user name>”, “MySQL password”);
In my case it is.
Where “cdcol” is database name, “root” is default MySQL username and MySQL password is blank. Here is complete code.
public static void main(String args)
Connection conn = null;
conn = DriverManager.getConnection("jdbc:mysql://localhost/test","root", "");
System.out.print("Database is connected !");
System.out.print("Do not connect to DB - Error:"+e);
Copy the code and paste it in your file. Let’s run it. In eclipse just hit the play button and it will compile and run it. Here is output of this code.
Well it runs, let’s see what happens when we stop our MySQL server and run the code. Stop MySQL database from XAMPP control panel and then run the code. I got this.
I hope you find this tutorial useful and in next upcoming tutorial we will deal with Java Servlet and many other cool stuff and again in easy way.
Become a pro!