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.
MySQL Connector/J 5.1 is a JDBC Type 4 driver that is compatible with the JDBC 3.0, JDBC 4.0, JDBC 4.1 and JDBC 4.2 specifications. The Type 4 designation means that the driver is a pure Java implementation of the MySQL protocol and does not rely on the MySQL client libraries. Web Development with Eclipse and Tomcat. Add the JAR files of the following libraries to the lib folder under the Tomcat directory: jstl.jar and standard.jar from JSP Standard Tag Library (JSTL). Mysql-connector-java--bin.jar from MySQL JDBC Driver. Download ktor-client-json-ios_debug_macos_x64 JAR file With dependencies Documentation Source code All Downloads are FREE. Search and download functionalities are using the official Maven repository.
To know it go to 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.
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. 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 java.sql.DriverManager; import java.sql.Connection; import java.sql.SQLException; 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. Class.forName(“com.mysql.jdbc.Driver”); 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:///Database”,””, “MySQL password”); In my case it is. DriverManager.getConnection(“jdbc:mysql://localhost/cdcol”,”root”, “”); Where “ cdcol” is database name, “ root” is default MySQL username and MySQL password is blank. Here is complete code.