crwdns121869:0crwdne121869:0

crwdns121871:0crwdne121871:0

crwdns121873:0crwdne121873:0

crwdns121875:0crwdne121875:0

  • crwdns121877:0crwdne121877:0

  • crwdns121879:0crwdne121879:0

  • crwdns121881:0crwdne121881:0

Warning

crwdns121883:0crwdne121883:0

crwdns121885:0crwdne121885:0

crwdns121887:0crwdne121887:0

import org.spongepowered.api.Sponge;
import org.spongepowered.api.service.sql.SqlService;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.sql.DataSource;

private SqlService sql;
public DataSource getDataSource(String jdbcUrl) throws SQLException {
    if (sql == null) {
        sql = Sponge.getServiceManager().provide(SqlService.class).get();
    }
    return sql.getDataSource(jdbcUrl);
}

// Later on
public void myMethodThatQueries() throws SQLException {
    String uri = "jdbc:h2:imalittledatabaseshortandstout.db";
    String sql = "SELECT * FROM test_tbl";

    try (Connection conn = getDataSource(uri).getConnection();
         PreparedStatement stmt = conn.prepareStatement(sql);
         ResultSet results = stmt.executeQuery()) {

        while (results.next()) {
            // ...
        }

    }

}

crwdns121889:0:javadoc:crwdne121889:0

crwdns121891:0crwdne121891:0

crwdns121893:0crwdne121893:0

crwdns121895:0crwdne121895:0