Connecting our application to a MySQL database using XAMPP is very important to make the data persistent. Your application must have a database like MySQL to store the data from the user or from the application itself.
For example, your application doesn’t have a database, and the user inputs their important information. After closing and reopening the application, you will notice that the data is gone because the application doesn’t have a database. There are so many databases to learn and integrate with your application.
In terms of development with a database, XAMPP is very useful for managing the database in development. XAMPP allows you to manage the Apache server and databases offline using PhpMyAdmin, at your localhost. Using XAMPP, you can connect your Java application to the MySQL database using XAMPP. Connecting your application to the MySQL database using XAMPP is very easy, but before doing that. We need to have XAMPP and MySQL JDBC driver to allow us to connect to the database.
To download the XAMPP application and MySQL JDBC driver, please proceed below.
Installing XAMPP on Windows
Installing XAMPP on Windows is very easy. After you download the application, install it. When the setup is running, click next to proceed to the installation. When you see the “Select Components,” I recommend you select all and click next. If you see “C:\xampp” in the installation folder, you can set the path you want, but it is up to you. Then click on next until the installation process is running.
After the installation, you can run XAMPP and see its control panel. The control panel has modules, and you can start the Apache server and MySQL server to make a database for development. To create a database, click on the admin button inline with MySQL, and you will be redirected to PhpMyAdmin localhost. Suppose you are ready to connect your Java application to the MySQL database using XAMPP. Please proceed below to learn more.
How to connect Java MySQL database using XAMPP
The first step is to create your Java project. Go to Libraries (right-click to add library) under your project below the Source packages in your Java project. Then, add the MySQL JDBC Driver. If you don’t have the library, click on the create button on the right side. Set the Library name and click OK. On the classpath tab, add the jar from the driver you have downloaded.
Navigate to the Services tab and right-click on Database to create a new connection. On the New Connection Wizard window, select the MySQL driver (connector / J driver).
In Customize connection, enter your database name and test the connection and copy the JDBC URL for later.
Code for the connection to the database.
public static void main(String[] args) { try{ Class.forName("com.mysql.cj.jdbc.Driver"); Connection con = DriverManager.getConnection("dbURL","username","password"); Statement state = con.createStatement(); System.out.println("connected"); }catch(Exception e){ e.printStackTrace(); } }
Thank you for sharing. Informative!