crwdns38906:0crwdne38906:0

crwdns38907:0crwdne38907:0

crwdns38908:0crwdne38908:0

crwdns38909:0crwdne38909:0

  • crwdns38910:0crwdne38910:0

  • crwdns38911:0crwdne38911:0

  • crwdns38912:0crwdne38912:0

Warning

crwdns38913:0crwdne38913:0

crwdns38914:0crwdne38914:0

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

    }

}

crwdns38916:0:javadoc:crwdne38916:0

crwdns38917:0crwdne38917:0

crwdns38918:0crwdne38918:0

crwdns38919:0crwdne38919:0