crwdns19036:0crwdne19036:0

crwdns19037:0crwdne19037:0

crwdns19038:0crwdne19038:0

crwdns19039:0crwdne19039:0

  • crwdns19040:0crwdne19040:0

  • crwdns19041:0crwdne19041:0

  • crwdns19042:0crwdne19042:0

Warning

crwdns19043:0crwdne19043:0

crwdns19044:0crwdne19044:0

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

    }

}

crwdns37100:0:javadoc:crwdne37100:0

crwdns37101:0crwdne37101:0

crwdns19047:0crwdne19047:0

crwdns42583:0crwdne42583:0