To connect XAMPP MySQL with Java IntelliJ, you can use the following steps:
-
Add the MySQL connector JAR file to your IntelliJ project:
-
Download the MySQL connector JAR file from the official website:
https://dev.mysql.com/downloads/connector/j/ - In IntelliJ, right-click on your project and select “Open Module Settings” or press “Ctrl + Alt + Shift + S”.
- In the “Project Structure” dialog, select “Modules” from the left panel.
- Select your module, then go to the “Dependencies” tab.
- Click on the “+” button and select “JARs or directories”.
- Find and select the downloaded MySQL connector JAR file.
- Click “OK” to apply the changes.
-
Download the MySQL connector JAR file from the official website:
-
Import required packages in your Java class:
<pre>
import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException;
</pre> -
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.