How to Connect Java to MySQL with JDBC

Enable Java Applications to Interact with the Database

java log
Java Logo

To Connect Java Application with your Database you need first to download Java Database Connectivity (JDBC) Driver in this example I will be using MySQL JDBC you can also try JDBC of Oracle.

JDBC Driver I used

Assuming that you already have your Database set up plus IDE ready in my case I'm using XAMPP and NetBeans.

Create connectDB() method

Copy Code
1
2// !!!   import this    !!!
3
4import java.sql.Connection;
5
6// in your CLASS
7// initialize variable myConn with type Connection
8
9public class ExampleConnectDatabase {
10
11   public static Connection myConn = null;
12
13   // connectDB method 
14   // creates connection to MySQL Database
15   public static void connectDB(){
16
17      try {
18
19         Class.forName("com.mysql.jdbc.Driver");
20         myConn = DriverManager.getConnection("jdbc:mysql://localhost:3306/dbentries", "root", "");
21
22         JOptionPane.showMessageDialog(null,"Connection Successfully Established.","Success", JOptionPane.INFORMATION_MESSAGE);
23
24      } catch (ClassNotFoundException | SQLException e) {
25
26         JOptionPane.showMessageDialog(null,"Database Connection Error." + e.getMessage(),"Error", JOptionPane.ERROR_MESSAGE);  
27
28      }
29
30   }
31
32} 
33
Always have LinkTo try...catch block whenever you're working with code that might throw exceptions.

Exceptions are unexpected errors that can occur during program execution.
By using try...catch, you can handle these errors gracefully and prevent your program from crashing.

Imports

1import java.sql.Connection;

This line imports the Connection class from the java.sql package, which is essential for establishing database connections in Java.

Variable Initialization

1public static Connection myConn = null;

This line declares LinkTo static variable named myConn of type Connection. It's initialized to null, indicating that no connection is established yet. The static keyword means that this variable is shared across all instances of the class.

Method Explanation

1Class.forName("com.mysql.jdbc.Driver");

This line loads the JDBC driver for MySQL. The driver is responsible for communicating with the database.

1myConn = DriverManager.getConnection("jdbc:mysql://localhost:3306/dbentries", "root", "");

This line establishes the actual connection to the database using the specific URL, username, and password.

1JOptionPane.showMessageDialog(...)

This code will display either LinkTo success message box if connected and an error message box with details if any exceptions occur.

Call it!

Copy Code
1connectDB();
Learn How to Execute Query with JDBC Driver

Created

  • Sun Mar 31 2024
  • coding

    java mysql connection

    jdbc driver

    database access

    java database

    data retrieval

    data storage

    data manipulation

    java persistence

    sql

    relational database

    Back on Top

    If you have any questions or feedback about this article feel free to email me here. Have a great day!

    More on LEARNINGS

      © 2024 Elly Mar