Summary
Add this file to your resource folder.
database.yml
# Database Library Author: Smudge
# File: database.yml
# Last Changed Library Version: 1.0.0
should_reconnect_every_cycle: true
reconnect_cooldown_millis: 2000
will_reconnect: true
time_between_requests_millis: 100
max_requests_pending: 40
sqlite:
enabled: true
path: "plugins/CozyJoinLeave/database.sqlite3"
mysql:
enabled: false
connection_string: "address:port"
database_name: ""
username: ""
password: ""
mongo:
enabled: false
connection_string: "" # example: "mongodb+srv://Name:Password@Name.?.mongodb.net/?retryWrites=true&w=majority"
database_name: ""
Load the config file and create the database.
Configuration databaseConfig = new YamlConfiguration(
this.getPlugin().getDataFolder(),
"database.yml"
);
databaseConfig.setResourcePath("database.yml");
databaseConfig.load();
Database database = new DatabaseBuilder(
this.databaseConfig
).create().connect();
public class ExampleRecord implements Record<Example2Record> {
public static final @NotNull String IDENTIFIER_KEY = "identifier";
public static final @NotNull String STRING_KEY = "value";
private final @Field(IDENTIFIER_KEY) @Primary @NotNull String identifier;
private @Field(STRING_KEY) String string;
public ExampleRecord(@NotNull String identifier) {
this.identifier = identifier;
}
@Override
public @NotNull ConfigurationSection convert() {
MemoryConfigurationSection section = new MemoryConfigurationSection();
section.set(STRING_KEY, string);
return section;
}
@Override
public @NotNull Example2Record convert(
@NotNull ConfigurationSection section) {
this.string = section.getString(STRING_KEY);
return this;
}
}
Define the table.
public class ExampleTable extends Table<ExampleRecord> {
public static final @NotNull String TABLE_NAME = "example";
@Override
public @NotNull String getName() {
return ExampleTable.TABLE_NAME;
}
@Override
public @NotNull ExampleRecord createEmpty(@NotNull PrimaryFieldMap identifiers) {
return new ExampleRecord(
identifiers.getString(ExampleRecord.IDENTIFIER_KEY)
);
}
}
Create the table in the database.
database.createTable(new ExampleTable());
ExampleRecord record = database.getTable(ExampleTable.class)
.getFirstRecord();
Last updated