Различные методы получения хоста локальной базы данных MariaDB

Чтобы получить хост локальной базы данных MariaDB, вы можете использовать различные методы в зависимости от используемого вами языка программирования. Ниже я приведу примеры кода для разных популярных языков:

  1. Python:

    import mysql.connector
    # Connect to the local MariaDB server
    connection = mysql.connector.connect(
    host="localhost",
    user="your_username",
    password="your_password",
    database="your_database"
    )
    # Retrieve the host information
    host = connection.server_host
    # Print the host
    print("Host:", host)
    # Close the connection
    connection.close()
  2. Java:

    import java.sql.*;
    public class Main {
    public static void main(String[] args) {
        Connection connection = null;
        try {
            // Connect to the local MariaDB server
            connection = DriverManager.getConnection(
                    "jdbc:mysql://localhost/your_database", "your_username", "your_password");
            // Retrieve the host information
            String host = connection.getMetaData().getURL();
            // Print the host
            System.out.println("Host: " + host);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // Close the connection
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    }
  3. PHP:

    <?php
    // Connect to the local MariaDB server
    $connection = mysqli_connect("localhost", "your_username", "your_password", "your_database");
    // Retrieve the host information
    $host = mysqli_get_host_info($connection);
    // Print the host
    echo "Host: " . $host;
    // Close the connection
    mysqli_close($connection);
    ?>
  4. Рубин:

    require 'mysql2'
    # Connect to the local MariaDB server
    client = Mysql2::Client.new(host: 'localhost', username: 'your_username', password: 'your_password', database: 'your_database')
    # Retrieve the host information
    host = client.info[:host]
    # Print the host
    puts "Host: #{host}"
    # Close the connection
    client.close