-
Notifications
You must be signed in to change notification settings - Fork 1
SQL
SimplixCore offers you to use a customizable library for sql operations. It's extensible and offers support for connection pools like HikariCP.
At first you need to add the following dependency to your pom file.
<dependency>
<groupId>dev.simplix.core</groupId>
<artifactId>simplixcore-database-sql</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
On Spigot & BungeeCord you don't need to shade this into your jar or load this as a library. It's bundled within the SimplixCore binary. Otherwise for standalone applications since there is no SimplixCore binary to load.
At first, you need to obtain an instance of SqlDatabaseConnection
. You create such an instance using the following static factory method:
SqlDatabaseConnection.hikari(host, user, password, port, database);
This will create a pooled connection to the database using HikariCP. If you want to customize some options you can instantiate the SqlDatabaseConnection
using its constructor:
new SqlDatabaseConnection(dataSource, host, user, password, port, database, connectionHandler);
Where dataSource
needs to be an instance of javax.sql.DataSource
and connectionHandler an instance of dev.simplix.core.database.sql.handlers.SqlConnectionHandler
.
The SqlDatabaseConnection class provides multiple methods to interact with the database.
The prepare-method is the easiest of them. It just executes a statement and does nothing return.
databaseConnection.prepare("UPDATE example SET name=? WHERE uuid=?", ps -> {
ps.setString(1, name);
ps.setString(2, uuid.toString());
});
The query method is used to retrieve data from the database.
Map<String, Object> data = databaseConnection.query("SELECT * FROM example WHERE name=?",
ps -> ps.setString(1, name), resultSet -> {
Map<String, Object> out = new HashMap<>();
out.put("uniqueId", resultSet.getString("uuid"));
out.put("lastJoin", resultSet.getTimestamp("lastJoin"));
out.put("firstJoin", resultSet.getTimestamp("firstJoin"));
return out;
});
As you can see, this method returns the object you will return using the ResultSetTransformer
. You can do this with every type.
2020 - SimplixSoftworks
- Introduction
- Getting started
- Maven Dependencies
- How to use the Dependency-Injection
- Localization
- Listener API
- SQL
- Libraries
- Utilities
- Contribute