Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Database Connectivity in Java with MySQL ~ foundjava

Jdbc Connection in Java with MySql




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 todownload 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.


This post first appeared on Found Java, please read the originial post: here

Share the post

Database Connectivity in Java with MySQL ~ foundjava

×

Subscribe to Found Java

Get updates delivered right to your inbox!

Thank you for your subscription

×