crwdns141439:0crwdne141439:0

Warning

crwdns141441:0crwdne141441:0

crwdns141443:0crwdne141443:0

crwdns141445:0crwdne141445:0

crwdns141447:0crwdne141447:0

  • crwdns141449:0crwdne141449:0

  • crwdns141451:0crwdne141451:0

  • crwdns141453:0crwdne141453:0

Warning

crwdns141455:0crwdne141455:0

crwdns141457:0crwdne141457:0

crwdns141459:0crwdne141459: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()) {
            // ...
        }

    }

}

crwdns141461:0:javadoc:crwdne141461:0

crwdns141463:0crwdne141463:0

crwdns141465:0crwdne141465:0

crwdns141467:0crwdne141467:0