Java Example Programs – How To Connect MySQL With Java

Hey Alien, In this post we will learn how to connect MySQL with Java.It’s a Java Example Programs.We will use JDBC Driver to connect our MySQL with our java Application. Database a very relative to all kind of our application. We can use standalone Java Application and also we can use with our java web application. So it’s really important to know how to connect MySQL with java.

So let’s Create a ToDo list that we are gonna follow all along in this post.

  1. First we will create a java application using eclipse .
  2. Then we will create a java file called driver.java
  3. then we will create a folder in our project folder called lib.
  4. then we will download MySQL JDBC driver called MySQL connector/j
  5. then copy the MySQL connector java jar file into the lib folder .
  6. then we will set our class build path in eclipse.

Then we are good to code And before we code we will create another ToDo list for our application.

So let’s follow the above mentioned step to get ready to code.

Step 1 : Create a java application using eclipse.

Open your eclipse and click File -> New -> Java Project Then type the project name JDBCDemo then click the Finish Button.
create a new java project

Step 2. Create a java file called driver.java .

Step 3. Create a folder in our project folder called lib

Step 4. Download MySQL JDBC driver called MySQL connector/j -> Click this link to get redirect to MySQL download center then choose your favorite environment then download.
The Link : https://dev.mysql.com/downloads/connector/j/

Step 5. Copy that mysql-connector-java-8.0.12.jar file into our lib folder.

Step 6. Then click out project folder -> properties -> Java Build Path -> Libraries -> Add Jars…

java build path
then click to apply and close . now your project is completely ready to write code for our java application that we want to connect with MySQL. Final Picture should be like this.

java application folder demo

Now our project ready , Open up your driver.java and write this code that mentioned below.

File: Driver.java

Here in our code you will see that we have 5 steps to create our application. Please let me remind you later those steps are followed.

  1. Get a connection to database
  2. Create a statement
  3. Execute SQL query
  4. Process the Result Set
  5. close the connection

OUTPUT :

I hope now you have a clear idea and concept that how actually step by step need to do create a java application.I hope you learned something today.If you have any queries or any clarification please comment’s below. we would love to hear from you. Thank you so much for being here in our practice house.

HAPPY CODING ….. HAPPY LIVING

 

Leave a Reply

Your email address will not be published. Required fields are marked *