java connect mariadb

java connect mariadb

To connect to a MariaDB database from Java, you will need to use a JDBC driver that supports MariaDB. One option is to use the MariaDB Connector/J driver, which is a JDBC driver for MariaDB written in pure Java.

To use the MariaDB Connector/J driver, you will need to download it and add it to your classpath. You can download the driver from the MariaDB website (https://mariadb.com/downloads/connector-j) or from the Maven Central repository (https://search.maven.org/search?q=g:org.mariadb.jdbc).

Once you have added the MariaDB Connector/J driver to your classpath, you can use the following code to connect to a MariaDB database from Java:

refer to:‮.iruttual‬com
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class Main {
  public static void main(String[] args) {
    String url = "jdbc:mariadb://localhost:3306/database_name";
    String username = "username";
    String password = "password";

    try (Connection conn = DriverManager.getConnection(url, username, password)) {
      System.out.println("Connected to the MariaDB database!");
    } catch (SQLException e) {
      System.out.println("Unable to connect to the MariaDB database: " + e.getMessage());
    }
  }
}

In this example, we use the DriverManager.getConnection method to establish a connection to the MariaDB database. The getConnection method takes three arguments: the URL of the database, the username, and the password. If the connection is successful, a Connection object is returned, which can be used to perform SQL queries and updates on the database.

Created Time:2017-11-03 00:14:51  Author:lautturi