This tutorial Java to MySQL Database Connection Source Code will help you on how you are going to create a connection within your Java application to your MySQL Database.

Image: BlackRock Business


As a programmer, it is required that you understand the database structure especially in MySQL. In MySQL you need to understand about the Server, user id, password, and database name. This details is needed to access a specific database. In my application, I used dbconnection as my class name.

Server: localhost
Database: mydatabase_name
User: root
Password: marjohn
Port: 3306


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import javax.swing.JOptionPane;
public class dbconnection {
Connection conn = null;
public static Connection db_config(){
try{
Class.forName("com.mysql.jdbc.Driver");
Connection conn= (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/mydatabase_name","root","marjohn");
return conn;
} catch (ClassNotFoundException | SQLException e) {
JOptionPane.showMessageDialog(null, e);
return null;
}
}
}