HomeJavaModsDatabase Utils
Database Utils
ModsJava

Database Utils

by KGriffon · on Modrinth

A simple mod for developers to make database usage easier. Free Minecraft Java mod for 26.1, 26.1.1, 26.1.2.

Which Minecraft do you have?

Every version of the game needs its own build. Pick yours, then download — the wrong build installs fine and then does nothing in the game. This mod is built for Fabric only.

⬇ Download for 26.1–26.2⬇ Download for 1.20–1.21.11
I don't know my version or loader
Open the Minecraft launcher and look at the profile you play on — it names both, like fabric-loader-1.21.4. The version also shows in the bottom-right corner of the game's main menu.
No loader in the profile name? Then it's plain Minecraft, and these mods won't run — you need Fabric or NeoForge installed first.

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();
Verified by MCModsHub

These come from our own check of the pack file, not from the source page.

Explore more