📅  最后修改于: 2023-12-03 15:01:29.174000             🧑  作者: Mango
MariaDB is a popular open source relational database management system. In this tutorial, we will learn how to connect to MariaDB database using Java.
Before proceeding, make sure you have the following tools installed on your system:
The first step is to create a new Java project in your favorite Integrated Development Environment (IDE). In this tutorial, we will use Eclipse IDE.
Follow the below steps to create a new Java project:
The next step is to add the MariaDB JDBC driver to the Java project. Follow the below steps to add the JDBC driver:
Now that we have added the JDBC driver to the project, we can use it to connect to the MariaDB database.
Use the below code snippet to establish a connection to the MariaDB database:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class MariaDbConnection {
public static void main(String[] args) {
Connection conn = null;
try {
conn = DriverManager.getConnection("jdbc:mariadb://localhost:3306/db_name", "user", "password");
System.out.println("Connection successful!");
} catch (SQLException e) {
System.out.println("Connection failed!");
e.printStackTrace();
} finally {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
Make sure to replace db_name
, user
, and password
with your own database details.
In this tutorial, we learned how to connect to MariaDB database using Java. We covered the steps to create a new Java project, add MariaDB JDBC driver to the project, and connect to the database.