Database Utils 图标

Database Utils

作者:rebot | 分类:模组

价格:0 墨喵币 下载量:0 点赞:0 版本 1.0.0+26.1
本资源为搬运资源,原资源地址: https://modrinth.com/mod/database-utils
资源信息

Minecraft 版本: 26.1 26.1.1 26.1.2 26.2

平台: fabric

标签: library storage

资源介绍

Database Utils


? Explanations

This mod includes some utilities for working with databases.
The most popular connectors are included directly in the mod JAR, which avoids having to import them into each mod and helps reduce the overall size of your projects.

To import this library, you must add the following to your build.gradle file.

repositories {
    maven { url "https://api.modrinth.com/maven" }
}

dependencies {
    implementation "maven.modrinth:database-utils:1.0.0"
}

?️ Usage

Configuration is managed by this library. By using the following line, a .properties file is automatically created (if it does not already exist) and loaded from the config folder.

Config config = new Config(MOD_ID);

In this example, the file will be named MOD_ID.properties.
Here is an example configuration file.

storage=postgres
host=127.0.0.1
port=5432
database=minecraft_db
user=user
password=password
Key Description
storage Database type. Must be one of: postgres, mysql, mariadb, sqlite, or test.
host IP address or hostname of the database server
port Port used by the database server.
database Name of the database.
user User used to connect to the database.
password Password of the user.

Then, you need to define your own class MyCustomDB, which extends DatabaseQueries.
This class is intended to contain all SQL related methods.
In this context, you have access to some method from DatabaseQueries, such as getType, which help you create an implementation compatible with multiple databases.

public class MyCustomDB extends DatabaseQueries {

    public MyCustomDB(Database db) {
        super(db);
    }

    @Override
    public void init() {
        try (Connection connection = getConnection()) {
            /* ... */
        } catch (SQLException | DatabaseLinkException e) {
            LOGGER.error("Unable to initialize: {}", e.getMessage());
        }
    }

    /* other methods */

    public void someMethod() {
        /* ... */
    }
}

You will also need to create a database instance. This is where the Config object is used. The following code snippet retrieves the database defined in the configuration and initializes your DatabaseQueries implementation.

Database database = DatabaseUtils.getDatabase(config);
MyCustomDB custom = new MyCustomDB(database);

You can now call any query defined in your DatabaseQueries implementation through your custom instance.

custom.someMethod();
下载太慢?试试 墨喵加速站,支持 Modrinth 和外网直链高速下载! 用的人越多,下载的越快!
下载与版本
评论(0)
登录 后发表评论。

暂无评论,抢个沙发吧~

举报此资源

请登录后举报

🔥 相关推荐
Cexy Lot

价格:0 墨喵币
下载量:0

查看详情
A Murder Mystery

价格:0 墨喵币
下载量:0

查看详情
HerdCraft Terraprism

价格:0 墨喵币
下载量:0

查看详情
Too Many Tools

价格:0 墨喵币
下载量:0

查看详情