We use cookies to improve your experience. If you continue to use our site, we'll assume that you're happy with it. :)

How to make Connection in Java JDBC with MySQL

 Java is one the most used language today. More than 10 million people use java for development.



To connect database to our java applications we generally use JDBC. JDBC stands for Java Database Connectivity.

To connect JDBC to MySQL we need MySQL connector, you can download this from MySQL website.

After downloading extract file and copy the jar file. Now open any IDE to code your java, and create a new project then create a directory and paste the JDBC jar file that you downloaded from MySQL and create new file and start typing code:


import java.sql.*;

public class MyJDBC {

    public static void main(String[] args) {

        try {

            Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/sakila", "root", "aakash");

            Statement statement = connection.createStatement();

            ResultSet resultSet = statement.executeQuery("select * from actor");

            while (resultSet.next()) {

                System.out.println(resultSet.getString("first_name"));

            }

        }

        catch(Exception e) {

            e.printStackTrace();

        }

    }

}


As you can see the above code, first thing is to import library of sql to our java program.

After importing library of sql create a class and a main method in which you will write codes.

Before going ahead you should know that to connect database to our java program you have to go through 3 processes, these are:-

1. Connection

2. Statement

3. ResultSet

Connection will track your sql files and help in running your code.

Statement will make connection to your database and try to connect the database.

ResultSet will execute the query which will be given to it.

After these processes we call a while loop to trace the every element of the query and print them.

Now you are good to go but good practice to connect a database is to code in try and catch statement, so keep your code in try and catch.

Now you are done! Run the program and you will get your solution.

In my case Output will be:

PENELOPE

NICK

ED

JENNIFER

JOHNNY

BETTE

GRACE

MATTHEW

JOE

CHRISTIAN

ZERO

KARL

UMA

VIVIEN

CUBA

FRED

HELEN

DAN

BOB

LUCILLE

KIRSTEN

ELVIS

SANDRA

CAMERON

KEVIN

RIP

JULIA

WOODY

ALEC

SANDRA

SISSY

TIM

MILLA

AUDREY

JUDY

BURT

VAL

TOM

GOLDIE

JOHNNY

JODIE

TOM

KIRK

NICK

REESE

PARKER

JULIA

FRANCES

ANNE

NATALIE

GARY

CARMEN

MENA

PENELOPE

FAY

DAN

JUDE

CHRISTIAN

DUSTIN

HENRY

CHRISTIAN

JAYNE

CAMERON

RAY

ANGELA

MARY

JESSICA

RIP

KENNETH

MICHELLE

ADAM

SEAN

GARY

MILLA

BURT

ANGELINA

CARY

GROUCHO

MAE

RALPH

SCARLETT

WOODY

BEN

JAMES

MINNIE

GREG

SPENCER

KENNETH

CHARLIZE

SEAN

CHRISTOPHER

KIRSTEN

ELLEN

KENNETH

DARYL

GENE

MEG

CHRIS

JIM

SPENCER

SUSAN

WALTER

MATTHEW

PENELOPE

SIDNEY

GROUCHO

GINA

WARREN

SYLVESTER

SUSAN

CAMERON

RUSSELL

MORGAN

MORGAN

HARRISON

DAN

RENEE

CUBA

WARREN

PENELOPE

LIZA

SALMA

JULIANNE

SCARLETT

ALBERT

FRANCES

KEVIN

CATE

DARYL

GRETA

JANE

ADAM

RICHARD

GENE

RITA

ED

MORGAN

LUCILLE

EWAN

WHOOPI

CATE

JADA

RIVER

ANGELA

KIM

ALBERT

FAY

EMILY

RUSSELL

JAYNE

GEOFFREY

BEN

MINNIE

MERYL

IAN

FAY

GRETA

VIVIEN

LAURA

CHRIS

HARVEY

OPRAH

CHRISTOPHER

HUMPHREY

AL

NICK

LAURENCE

WILL

KENNETH

MENA

OLYMPIA

GROUCHO

ALAN

MICHAEL

WILLIAM

JON

GENE

LISA

ED

JEFF

MATTHEW

DEBBIE

RUSSELL

HUMPHREY

MICHAEL

JULIA

RENEE

ROCK

CUBA

AUDREY

GREGORY

JOHN

BURT

MERYL

JAYNE

BELA

REESE

MARY

JULIA

THORA

Post a Comment