Connecting Java to MySQL Database Java Tutorial


Connecting Java to MySQL Database Java Tutorial

This tutorial is all about Connecting Java to MySQL Database Java Tutorial. In this tutorial you will learn how to connect your java project into database.

It is very useful when creating a project that data need to be stored in database. Because it allows you to select, update, delete, and insert your data in your database. This tutorial used sqlitejdbc-v056 or other version to make this tutorial work. Please follow all the steps below to complete this tutorial.

Connecting Java to MySQL Databse in Java steps.

  • Create your class in your project. The class name should start with capital letter.
  • At the top of your class add your import package , using import java.sql.*;
  • Declare instance of Connection , using Connection conn = null
  • Create a method, name it whatever you want. And make sure it is a static method
  • Declare your try/catch block to catch the error throws an exception (OPTIONAL)
  • Copy the codes below and paste it into your program. The following codes are used in accessing database connectivity.
  • Advertisement
  • In get Connection Function the first parameter is your  host and database and second parameter is your username and last parameter is your paassword


import java.sql.*;

public class HelloWorld {

Connection conn = null;


public static Connection ConnectToDb(){



Connection conn = DriverManager.getConnection(“jdbc:mysql://localhost/yourdatabase”,”yourusername”,”yourpassword”);

return conn;

}catch(Exception e){


return null;





For questions or any other concerns or thesis/capstone creation with documentation, you can contact me through the following:

E-Mail: [email protected]


Contact No.: +639098911050

Ryan A. Salem

BSIT Graduate, soon to be MIT.

Java Developer / System Developer

Related topic(s) that you make like:

Looking for more source code? Type your keyword here here!

Leave a Reply

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