💾Database loader

Database loader loads data into a list from SQL database.

Database loader must extend AbstractSqlDataBaseLoader.

Supported databases:

  • MySQL

  • SQLite

AbstractSqlDataBaseloader methods:

String getTableName(); //Gets table name
String getTableNameWithPrefix() //Gets table name with prefix
void load(String tableName); //Loads whole table

How to create a database loader?

First of all, get the current SQL database, you can do it thanks to T-DataBasesAPI.

The prefix of the table will be added automatically.

public class MyDatabaseLoader extends AbstractSqlDataBaseLoader<MyUser> {
    
    public MyDatabaseLoader (SQLDatabaseAdapter sqlDatabaseAdapter) {
        super(sqlDatabaseAdapter);
    }
    
    /**
     * Loads data from the database
     */
    @Override
    public void load(String tableName) {
        QueryBuilder queryBuilder = new QueryBuilder();

        try (ResultSet resultSet = sqlDatabaseAdapter.selectAll(getTableNameWithPrefix())) {
            while (resultSet.next()) {
                MyUser user = new MyUser(
                        UUID.fromString(resultSet.getString("uuid")),
                        resultSet.getInt("coins")
                );

                getData().add(user);
            }
        } catch (ExecutionException | InterruptedException | SQLException e) {
            throw new RuntimeException(e);
        }
    }
    
    protected String getTableName(){
        return MyUser.TABLE_NAME;
    }
}

Last updated