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.
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
![]() |
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
} |
0 Comments