crwdns151515:0crwdne151515:0
crwdns151517:0crwdne151517:0
crwdns151519:0crwdne151519:0
crwdns151521:0crwdne151521:0
crwdns151523:0crwdne151523:0
crwdns151525:0crwdne151525:0
crwdns151527:0crwdne151527:0
Warning
crwdns151529:0crwdne151529:0
crwdns151531:0crwdne151531:0
crwdns151533:0crwdne151533: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()) {
// ...
}
}
}
crwdns151535:0:javadoc:crwdne151535:0
crwdns151537:0crwdne151537:0
crwdns151539:0crwdne151539:0
crwdns151541:0crwdne151541:0