JDBC and MySQL Connection tutorial.

Hello Friends,

One of the significant characteristic of any programming language is its connectivity with the database and its operations. Java supports many options to connect to the database and one of the most used database is MySql. JDBC (Java Data Base Connectivity) is used for connecting the java code with the database and it provides cross-DBMS connectivity to a wide range of SQL databases and access to other tabular data sources, such as spreadsheets or flat files. This tutorial shows the jdbc and mySql connection establishment and its use.

You may know, both JDBC and MySQL are freely available for many purposes, the combination of JDBC and MySQL is a powerful combination that should be of interest for a wide variety of applications. This tutorial is about connecting the MySql database with the java code through JDBC.

For this tutorial, I have used the database named code2java and the table that we are using is users.

As you can see, the table contains 2 records with 5 different information. I have used this table for displaying its contents via the java code.

In the following code, “Class.forName(driver).newInstance()” creates the new instance of the JDBC driver and DriverManager.getConnection() function connects to the database. It takes three variable which are required for setting up the connection those are – url to the database, username and password.

A Statement is an interface that represents a SQL statement. You can execute the  Statement objects, and they generates the result, these results are called as the ResultSet objects which represents the table from the database. To get the result from the Statement you need the connection object.

The remaining code is self explanatory.


Run the program and see the output.

Hope this will help you.



Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.