Skip to content

Database connection pooling utility and API for communication with databases between MineCrossing services

Notifications You must be signed in to change notification settings

MineCrossing/DatabaseConnector

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 

Repository files navigation

DatabaseConnector

Database connection pooling utility and API for communication with databases between MineCrossing services.

DatabaseConnector uses connection pooling to simplify the process of managing multiple plugins connecting to the same database.

Maven Repository

To use RedisAPI run the following commands:

git clone https://github.com/MineCrossing/DatabaseConnector.git
cd DatabaseConnector
mvn install

Then add this into your dependencies section in your pom.xml

<dependency>
    <groupId>xyz.minecrossing</groupId>
    <artifactId>DatabaseConnector</artifactId>
    <version>1.0.0</version>
</dependency>

Example Usage

Note: Database calls should always be done asynchronously to avoid blocking the main thread.

/**
 * Get a user's balance
 * 
 * @param user The user who's balance to get
 * @return The user's balance
*/
public long getMoney(User user) {
    long money = -1; // default money to -1

    try (Connection con = DatabaseConnector.getInstance().getConnection("database")) { // establish a connection with the database named "database"
        PreparedStatement ps = con.prepareStatement("SELECT money FROM money WHERE uuid = ? LIMIT 1;"); // create a prepared statement with a custom SQL query
        ps.setString(1, user.getUUID().toString()); // prepared statement params start at 1
            
        ResultSet rs = ps.executeQuery(); // execute the prepared statement query and store it in a ResultSet
        while (rs.next()) { // while the ResultSet has content
            money = rs.getLong("money"); // assign the money variable to be the value of the money column
        }
        ps.close(); // close the prepared statement
        rs.close(); // close the result set
        con.close(); // close the DB connection (IntelliJ wines, ignore it)
    } catch (SQLException e) {
        e.printStackTrace(); // catch any sql exceptions and print them to console
    }

    return money; // return the user's balance
}

About

Database connection pooling utility and API for communication with databases between MineCrossing services

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages