How to connect xampp mysql with java intellij

To connect XAMPP MySQL with Java IntelliJ, you can use the following steps:

  1. Add the MySQL connector JAR file to your IntelliJ project:

    1. Download the MySQL connector JAR file from the official website:
      https://dev.mysql.com/downloads/connector/j/
    2. In IntelliJ, right-click on your project and select “Open Module Settings” or press “Ctrl + Alt + Shift + S”.
    3. In the “Project Structure” dialog, select “Modules” from the left panel.
    4. Select your module, then go to the “Dependencies” tab.
    5. Click on the “+” button and select “JARs or directories”.
    6. Find and select the downloaded MySQL connector JAR file.
    7. Click “OK” to apply the changes.
  2. Import required packages in your Java class:

            <pre>
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
            </pre>
          
  3. Establish a connection to the MySQL database:

            <pre>
    public class Main {
        public static void main(String[] args) {
            String url = "jdbc:mysql://localhost:3306/your_database_name"; // Replace "your_database_name" with your actual database name
            String user = "root"; // Replace with your MySQL username
            String password = ""; // Replace with your MySQL password
    
            try {
                Connection connection = DriverManager.getConnection(url, user, password);
                System.out.println("Connected to MySQL database!");
                // Perform database operations here
                connection.close(); // Close the connection
            } catch (SQLException e) {
                System.out.println("Connection failed!");
                e.printStackTrace();
            }
        }
    }
            </pre>
          

    Note: Make sure to replace “your_database_name”, and input your MySQL username and password in the above code.

This is a basic example for connecting XAMPP MySQL with Java IntelliJ. You can modify the code according to your specific requirements and perform various database operations using the established connection.

Leave a comment