Let's learn java programming language with easy steps

Friday, 12 January 2018

Database Connectivity in Java with MySQL

Jdbc Connection in Java with MySql

mysql jdbc example

Now here, We are going to perform database connectivity in java with mysql line-by-line with simple example .

Here we will see first mysql jdbc example and then explanation of this example.

If you want to perform java jdbc connectivity with mysql database then you have to download mysql and install in your computer first and after installing mysql db. 

You have to download mysqlconnector.jar file or it can be comes with mysql package and then you have to set path(temporary or permanent path) of mysql connector java jar.


There are 3 ways to load the jar file

1) First way

paste the mysqlconnector.jar file into jre/lib/ext folder.

2) Second way - set temporary classpath

Open cmd and type : 

C:>set classpath =c:\mysql\mysql-connector-java-5.1.45-bin.jar;.;

3) Third way - set permanent path

Go to in MyComputer properties and then environment variables, click new tab and type classpath in variable name box and in variable value paste the mysqlconnector.jar file e.g c:\mysql\mysql-connector-java-5.1.45-bin.jar;.;

Let's start simple jdbc program in java using mysql database.

Let's create database and table in mysql.

create database anurag;//create db in mysql

use anurag;//select database

create table student(rollno int(10), name varchar(40));//create table


Java MySql Connection Example

In this mysql jdbc example we will fetch all the records from the student table.

import java.sql.*;
class MySqlJDBCExample
{
public static void main(String args[])
{
try
{
//Step 1, load driver class
Class.forName("com.mysql.jdbc.Driver");

//Step 2, create connection object
Connection con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/anurag","root","root");

//Step 3, create statement object
Statement stm = con.createStatement();

//Step 4, execute query
ResultSet rs = stm.executeQuery("select * from student");

while(rs.next())
{
System.out.println(rs.getInt(1)+" "+rs.getString(2));
}

//Step 5, close connection object
con.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}


Explanation of above java jdbc mysql example

If you want to connect java program with mysql database using jdbc then you have to follow 5 steps e.g...

Driver Class : com.mysql.jdbc.Driver

Load the driver class.

Connection Url : jdbc:mysql://localhost:3306/anurag

Where jdbc is an api and mysql is the database and localhost is the server name in which mysql is running, we an use IP address also and 3306 is the port number and anurag is the database name, you can also select other databses.

Username : By default the "root" is the username of mysql database.

Password : User can give any password at the time of mysql instalation but in the above program we used root as the password.

You can check - How to connect java application with oracle database using jdbc.

Here we learned database connectivity in java with mysql using jdbc API.

Share:

2 comments:

  1. This post is awesome for java beginners to learn how to connect mysql database through jdbc in java.

    Thnks

    ReplyDelete
  2. thanks for updating the information, it's very useful for java freshers
    For any IT Jobs Click Here

    ReplyDelete

Facebook Page Likes

Follow javatutorial95 on Google+

Follow javatutorial95 on twitter

Popular Posts

Get Updates By Email

Link

Translate