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.

package com.JavaExamples;
import java.sql.*;

public class JDBCConnectionExample
{
public static void main(String[] args)
{
System.out.println("MySQL & JDBC Connect Example.");
Connection conn = null;
String url = "jdbc:mysql://localhost:3306/";
String dbName = "code2java";
String driver = "com.mysql.jdbc.Driver";
String userName = "root";
String password = "root";
String dbtime = null;
try
{
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url+dbName,userName,password);
System.out.println("Database Connected");
Statement stmt = conn.createStatement();
String query="Select * from users";
ResultSet rs = stmt.executeQuery(query);
while (rs.next())
{
int srNo = rs.getInt("sr_no");
String fName = rs.getString("firstName");
String lName = rs.getString("lastName");
String uID = rs.getString("uname");
String Pwd = rs.getString("password");
System.out.println("| Sr No: "+srNo+ " | FirstName: "+fName+" | LastName: "+lName+" | UserName: "+uID+" | Password: "+Pwd+" |");
}
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
//This is used for Disconnecting..
try
{
conn.close();
System.out.println("Database Disconnected");
}
catch (SQLException e)
{
e.printStackTrace();
System.out.println("Error while Disconnecting from database");
}

}
}
}

 

Run the program and see the output.

Hope this will help you.

Regards,

admin@code2java.com

Related Posts

  • MySql Installation

    Hello Friends,   This is one of my tutorials related to database. You all must be familiar with MySQL, but still for those who have not learned about this, for them I will recall it in short. MySQL is a relational database management system (RDBMS) that runs as a server providing multi-user access to a number of databases.The…

  • Abstract Class In JAVA

    Hello Friends, This tutorial is for all the Java followers. One of the best feature that is widely used is the term ‘Abstract’. This term can be used as either class or a simple method. An abstract method is any method that is just declared but not instantiated. In other words one can just create…

  • Threads in Java.

    Hello Friends, This is the tutorial for the java developers. One of the most significance feature of core java is Threading. Threading deals with the processing of Threads in a single java program. Let us learn what actually are Threads. *What are Threads? Threads are independently running processes that are isolated from each other upto…

  • Collections In Java.

    Hello friends, Welcome to another tutorial for java followers. You all may have heard about Collections, it is one of the amazing feature in java. Collections are the object for the group of elements, these elements are nothing but the different data structures like as Array Lists, Linked Lists, Vectors, Hash tables,Hash List, Trees, Hash…

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.