下面列出了java.sql.ResultSet#isClosed ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public List<T> processResultSet(ResultSet resultSet) {
try {
if ((resultSet == null) || resultSet.isClosed()) {
return new ArrayList<>();
}
List<T> databaseObjects = new ArrayList<>();
while (resultSet.next()) {
T databaseObject = processSingleResultAllColumns(resultSet);
databaseObjects.add(databaseObject);
}
return databaseObjects;
}
catch (Exception e) {
logger.error(e.toString() + System.lineSeparator() + StackTrace.getStringFromStackTrace(e));
return new ArrayList<>();
}
}
@Override
public OutputBlacklist processSingleResultAllColumns(ResultSet resultSet) {
try {
if ((resultSet == null) || resultSet.isClosed()) {
return null;
}
Integer id = resultSet.getInt("ID");
if (resultSet.wasNull()) id = null;
Integer mgId = resultSet.getInt("METRIC_GROUP_ID");
if (resultSet.wasNull()) mgId = null;
OutputBlacklist outputBlacklist = new OutputBlacklist(id, mgId);
return outputBlacklist;
}
catch (Exception e) {
logger.error(e.toString() + System.lineSeparator() + StackTrace.getStringFromStackTrace(e));
return null;
}
}
@Override
public MetricLastSeen processSingleResultAllColumns(ResultSet resultSet) {
try {
if ((resultSet == null) || resultSet.isClosed()) {
return null;
}
String metricKeySha1 = resultSet.getString("METRIC_KEY_SHA1");
if (resultSet.wasNull()) metricKeySha1 = null;
String metricKey = resultSet.getString("METRIC_KEY");
if (resultSet.wasNull()) metricKey = null;
Timestamp lastModified = resultSet.getTimestamp("LAST_MODIFIED");
if (resultSet.wasNull()) lastModified = null;
MetricLastSeen metricLastSeen = new MetricLastSeen(metricKeySha1, metricKey, lastModified);
return metricLastSeen;
}
catch (Exception e) {
logger.error(e.toString() + System.lineSeparator() + StackTrace.getStringFromStackTrace(e));
return null;
}
}
@Override
public MetricGroupTag processSingleResultAllColumns(ResultSet resultSet) {
try {
if ((resultSet == null) || resultSet.isClosed()) {
return null;
}
Integer id = resultSet.getInt("ID");
if (resultSet.wasNull()) id = null;
Integer mgId = resultSet.getInt("METRIC_GROUP_ID");
if (resultSet.wasNull()) mgId = null;
String tag = resultSet.getString("TAG");
if (resultSet.wasNull()) tag = null;
MetricGroupTag metricGroupTag = new MetricGroupTag(id, mgId, tag);
return metricGroupTag;
}
catch (Exception e) {
logger.error(e.toString() + System.lineSeparator() + StackTrace.getStringFromStackTrace(e));
return null;
}
}
private void tryCloseConnection(Connection connection, PreparedStatement ps, ResultSet resultSet) {
try {
if (resultSet != null && !resultSet.isClosed()) {
resultSet.close();
}
if (ps != null && !ps.isClosed()) {
ps.close();
}
if (connection != null && !connection.isClosed()) {
connection.close();
}
} catch (SQLException ex) {
throw new JdbcTemplateException("Cannot close the database", ex);
}
}
/**
* Replays the tuples in sync mode for replayed windows
*/
public void emitReplayedTuples(PreparedStatement ps)
{
ResultSet rs = null;
try (PreparedStatement pStat = ps;) {
pStat.setFetchSize(getFetchSize());
rs = pStat.executeQuery();
if (rs == null || rs.isClosed()) {
return;
}
while (rs.next()) {
emitTuple(getTuple(rs));
lastEmittedRow++;
}
} catch (SQLException ex) {
throw new RuntimeException(ex);
}
}
@Override
public void closeInvoked() {
//should we cache it
boolean shouldClose = true;
if (cacheSize.get() < maxCacheSize) {
//cache a proxy so that we don't reuse the facade
CachedStatement proxy = new CachedStatement(getDelegate(),getSql());
proxy.setCacheKey(getCacheKey());
try {
// clear Resultset
ResultSet result = getDelegate().getResultSet();
if (result != null && !result.isClosed()) {
result.close();
}
// clear parameter
getDelegate().clearParameters();
//create a new facade
Object actualProxy = getConstructor().newInstance(new Object[] { proxy });
proxy.setActualProxy(actualProxy);
proxy.setConnection(getConnection());
proxy.setConstructor(getConstructor());
if (cacheStatement(proxy)) {
proxy.cached = true;
shouldClose = false;
}
} catch (Exception x) {
removeStatement(proxy);
}
}
if (shouldClose) {
super.closeInvoked();
}
closed = true;
delegate = null;
}
public boolean hasLevel30Character() {
PreparedStatement ps = null;
ResultSet rs = null;
Connection con = null;
try {
con = DatabaseConnection.getConnection();
ps = con.prepareStatement("SELECT `level` FROM `characters` WHERE accountid = ?");
ps.setInt(1, getPlayer().getAccountID());
rs = ps.executeQuery();
while (rs.next()) {
if (rs.getInt("level") >= 30) {
ps.close();
rs.close();
return true;
}
}
} catch (SQLException sqle) {
sqle.printStackTrace();
} finally {
try {
if (ps != null && !ps.isClosed()) {
ps.close();
}
if (rs != null && !rs.isClosed()) {
rs.close();
}
if (con != null && !con.isClosed()) {
con.close();
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}
return getPlayer().getLevel() >= 30;
}
/**
* Executes a prepared statement.
*
* @param signature Parsed statement
* @param isUpdate if the execute is for an update
*
* @return as specified by {@link java.sql.Statement#execute(String)}
* @throws java.sql.SQLException if a database error occurs
*/
protected boolean executeInternal(Meta.Signature signature, boolean isUpdate)
throws SQLException {
ResultSet resultSet = executeQueryInternal(signature, isUpdate);
// user may have cancelled the query
if (resultSet.isClosed()) {
return false;
}
return true;
}
/**
* 关闭ResultSet
* @param rs
* @throws HongsException
*/
public void closeResultSet(ResultSet rs)
throws HongsException
{
try
{
if (rs == null || rs.isClosed()) return;
rs.close();
}
catch (SQLException ex)
{
throw new HongsException(0x1035, ex);
}
}
@Override
public void closeInvoked() {
//should we cache it
boolean shouldClose = true;
if (cacheSize.get() < maxCacheSize) {
//cache a proxy so that we don't reuse the facade
CachedStatement proxy = new CachedStatement(getDelegate(),getSql());
proxy.setCacheKey(getCacheKey());
try {
// clear Resultset
ResultSet result = getDelegate().getResultSet();
if (result != null && !result.isClosed()) {
result.close();
}
//create a new facade
Object actualProxy = getConstructor().newInstance(new Object[] { proxy });
proxy.setActualProxy(actualProxy);
proxy.setConnection(getConnection());
proxy.setConstructor(getConstructor());
if (cacheStatement(proxy)) {
proxy.cached = true;
shouldClose = false;
}
} catch (Exception x) {
removeStatement(proxy);
}
}
if (shouldClose) {
super.closeInvoked();
}
closed = true;
delegate = null;
}
@Override
public NotificationGroup processSingleResultAllColumns(ResultSet resultSet) {
try {
if ((resultSet == null) || resultSet.isClosed()) {
return null;
}
Integer id = resultSet.getInt("ID");
if (resultSet.wasNull()) id = null;
String name = resultSet.getString("NAME");
if (resultSet.wasNull()) name = null;
String uppercaseName = resultSet.getString("UPPERCASE_NAME");
if (resultSet.wasNull()) uppercaseName = null;
String emailAddresses = resultSet.getString("EMAIL_ADDRESSES");
if (resultSet.wasNull()) emailAddresses = null;
NotificationGroup notificationGroup = new NotificationGroup(id, name, uppercaseName, emailAddresses);
return notificationGroup;
}
catch (Exception e) {
logger.error(e.toString() + System.lineSeparator() + StackTrace.getStringFromStackTrace(e));
return null;
}
}
@Override
public MetricGroup processSingleResultAllColumns(ResultSet resultSet) {
try {
if ((resultSet == null) || resultSet.isClosed()) {
return null;
}
Integer id = resultSet.getInt("ID");
if (resultSet.wasNull()) id = null;
String name = resultSet.getString("NAME");
if (resultSet.wasNull()) name = null;
String uppercaseName = resultSet.getString("UPPERCASE_NAME");
if (resultSet.wasNull()) uppercaseName = null;
String description = resultSet.getString("DESCRIPTION");
if (resultSet.wasNull()) description = null;
MetricGroup metricGroup = new MetricGroup(id, name, uppercaseName, description);
return metricGroup;
}
catch (Exception e) {
logger.error(e.toString() + System.lineSeparator() + StackTrace.getStringFromStackTrace(e));
return null;
}
}
@Override
public Gauge processSingleResultAllColumns(ResultSet resultSet) {
try {
if ((resultSet == null) || resultSet.isClosed()) {
return null;
}
String bucketSha1 = resultSet.getString("BUCKET_SHA1");
if (resultSet.wasNull()) bucketSha1 = null;
String bucket = resultSet.getString("BUCKET");
if (resultSet.wasNull()) bucket = null;
BigDecimal metricValue = resultSet.getBigDecimal("METRIC_VALUE");
if (resultSet.wasNull()) metricValue = null;
Timestamp lastModified = resultSet.getTimestamp("LAST_MODIFIED");
if (resultSet.wasNull()) lastModified = null;
Gauge gauge = new Gauge(bucketSha1, bucket, metricValue, lastModified);
return gauge;
}
catch (Exception e) {
logger.error(e.toString() + System.lineSeparator() + StackTrace.getStringFromStackTrace(e));
return null;
}
}
public static boolean isResultSetValid(ResultSet results) {
try {
if ((results == null) || results.isClosed()) {
return false;
}
else {
return true;
}
}
catch (Exception e) {
logger.error(e.toString() + System.lineSeparator() + StackTrace.getStringFromStackTrace(e));
return false;
}
}
private static AssertionError addRsToReport(
AssertionError afe,
ResultSetMetaData rsmd,
List<List<String>> seen,
List<String> seenRow,
ResultSet rs) throws SQLException {
try {
if (rs == null) {
return new AssertionError(
afe.getMessage() + "\n<NULL>", afe);
}
final int c = rsmd.getColumnCount();
StringBuilder heading = new StringBuilder(" ");
StringBuilder underline = new StringBuilder(" ");
// Display column headings
for (int i = 1; i <= c; i++) {
if (i > 1) {
heading.append(",");
underline.append(" ");
}
int len = heading.length();
heading.append(rsmd.getColumnLabel(i));
len = heading.length() - len;
for (int j = len; j > 0; j--) {
underline.append("-");
}
}
heading.append("\n");
underline.append("\n");
StringBuilder rowImg = new StringBuilder();
rowImg.append(afe.getMessage()).
append("\n\n").
append(heading.toString()).
append(underline.toString());
if (!rs.isClosed()) {
final int s = seenRow.size();
// Get any rest of columns of current row
for (int i = 0; i < c - s; i++) {
String column = null;
try {
column = rs.getString(s + i + 1);
} catch (SQLException e) {
// We may not yet have called next?
if (e.getSQLState().equals("24000")) {
if (rs.next()) {
column = rs.getString(s + i + 1);
} else {
break;
}
}
}
seenRow.add(column);
}
if (seenRow.size() > 0) {
seen.add(new ArrayList<>(seenRow));
seenRow.clear();
}
// Get any remaining rows
while (rs.next()) {
for (int i = 0; i < c; i++) {
seenRow.add(rs.getString(i + 1));
}
seen.add(new ArrayList<>(seenRow));
seenRow.clear();
}
}
// Display data
for (List<String> row : seen) {
rowImg.append(" ").
append(row.toString()).
append("\n");
}
return new AssertionError(rowImg.toString(), afe);
} catch (Throwable t) {
// Return the original error.
return afe;
}
}
private MultiShardException validateResultSet(ResultSet r,
ShardLocation loc) throws SQLException {
if (r.isClosed()) {
// ResultSet is already closed. Hence adding an exception in its place.
return new MultiShardException(loc, new MultiShardResultSetClosedException(
String.format("The result set for '%1$s' was closed and could not be added.", loc.getDatabase())));
}
ResultSetMetaData m = r.getMetaData();
if (m == null || m.getColumnCount() == 0) {
// ResultSet does not have proper metadata to read.
return new MultiShardException(loc, new MultiShardResultSetInternalException(
String.format("The result set for '%1$s' does not have proper metadata to read and could not be added.", loc.getDatabase())));
}
if (this.schemaComparisonTemplate == null) {
this.schemaComparisonTemplate = r.getMetaData();
return null;
}
for (int i = 1; i <= m.getColumnCount(); i++) {
// Get the designated column's name.
String expectedName = this.schemaComparisonTemplate.getColumnName(i);
String actualName = m.getColumnName(i);
if (!Objects.equals(expectedName, actualName)) {
return new MultiShardException(loc, new MultiShardSchemaMismatchException(loc,
String.format("Expected schema column name %1$s, but encountered schema column name %2$s.", expectedName, actualName)));
}
// Retrieves the designated column's SQL type.
if (!Objects.equals(this.schemaComparisonTemplate.getColumnType(i), m.getColumnType(i))) {
return new MultiShardException(loc,
new MultiShardSchemaMismatchException(loc,
String.format("Mismatched SQL type values for column %1$s. Expected: %2$s. Actual: %3$s", actualName,
this.schemaComparisonTemplate.getColumnTypeName(i), m.getColumnTypeName(i))));
}
// Get the designated column's specified column size.
int expectedPrecision = this.schemaComparisonTemplate.getPrecision(i);
int actualPrecision = m.getPrecision(i);
if (!Objects.equals(expectedPrecision, actualPrecision)) {
return new MultiShardException(loc,
new MultiShardSchemaMismatchException(loc,
String.format("Mismatched nullability values for column %1$s. Expected: %2$s. Actual: %3$s", actualName,
expectedPrecision, actualPrecision)));
}
// Indicates the nullability of values in the designated column.
int expectedNullableValue = this.schemaComparisonTemplate.isNullable(i);
int actualNullableValue = m.isNullable(i);
if (!Objects.equals(expectedNullableValue, actualNullableValue)) {
return new MultiShardException(loc,
new MultiShardSchemaMismatchException(loc,
String.format("Mismatched nullability values for column %1$s. Expected: %2$s. Actual: %3$s", actualName,
NullableValue.forValue(expectedNullableValue), NullableValue.forValue(actualNullableValue))));
}
}
return null;
}
public final List<MonsterDropEntry> retrieveDrop(final int monsterId) {
if (drops.containsKey(monsterId)) {
return drops.get(monsterId);
}
final List<MonsterDropEntry> ret = new LinkedList<>();
PreparedStatement ps = null;
ResultSet rs = null;
Connection con = null;
try {
con = DatabaseConnection.getConnection();
ps = con.prepareStatement("SELECT itemid, chance, minimum_quantity, maximum_quantity, questid FROM drop_data WHERE dropperid = ?");
ps.setInt(1, monsterId);
rs = ps.executeQuery();
while (rs.next()) {
ret.add(new MonsterDropEntry(rs.getInt("itemid"), rs.getInt("chance"), rs.getInt("minimum_quantity"), rs.getInt("maximum_quantity"), rs.getShort("questid")));
}
con.close();
} catch (SQLException e) {
e.printStackTrace();
return ret;
} finally {
try {
if (ps != null && !ps.isClosed()) {
ps.close();
}
if (rs != null && !rs.isClosed()) {
rs.close();
}
if (con != null && !con.isClosed()) {
con.close();
}
} catch (SQLException ignore) {
ignore.printStackTrace();
return ret;
}
}
drops.put(monsterId, ret);
return ret;
}
public CashShop(int accountId, int characterId, int jobType) throws SQLException {
this.accountId = accountId;
this.characterId = characterId;
if (!YamlConfig.config.server.USE_JOINT_CASHSHOP_INVENTORY) {
if (jobType == 0) {
factory = ItemFactory.CASH_EXPLORER;
} else if (jobType == 1) {
factory = ItemFactory.CASH_CYGNUS;
} else if (jobType == 2) {
factory = ItemFactory.CASH_ARAN;
}
} else {
factory = ItemFactory.CASH_OVERALL;
}
Connection con = DatabaseConnection.getConnection();
PreparedStatement ps = null;
ResultSet rs = null;
try {
ps = con.prepareStatement("SELECT `nxCredit`, `maplePoint`, `nxPrepaid` FROM `accounts` WHERE `id` = ?");
ps.setInt(1, accountId);
rs = ps.executeQuery();
if (rs.next()) {
this.nxCredit = rs.getInt("nxCredit");
this.maplePoint = rs.getInt("maplePoint");
this.nxPrepaid = rs.getInt("nxPrepaid");
}
rs.close();
ps.close();
for (Pair<Item, MapleInventoryType> item : factory.loadItems(accountId, false)) {
inventory.add(item.getLeft());
}
ps = con.prepareStatement("SELECT `sn` FROM `wishlists` WHERE `charid` = ?");
ps.setInt(1, characterId);
rs = ps.executeQuery();
while (rs.next()) {
wishList.add(rs.getInt("sn"));
}
rs.close();
ps.close();
con.close();
} finally {
if (ps != null && !ps.isClosed()) ps.close();
if (rs != null && !rs.isClosed()) rs.close();
if (con != null && !con.isClosed()) con.close();
}
}
@Override
public Suspension processSingleResultAllColumns(ResultSet resultSet) {
try {
if ((resultSet == null) || resultSet.isClosed()) {
return null;
}
Integer id = resultSet.getInt("ID");
if (resultSet.wasNull()) id = null;
String name = resultSet.getString("NAME");
if (resultSet.wasNull()) name = null;
String uppercaseName = resultSet.getString("UPPERCASE_NAME");
if (resultSet.wasNull()) uppercaseName = null;
String description = resultSet.getString("DESCRIPTION");
if (resultSet.wasNull()) description = null;
Boolean isEnabled = resultSet.getBoolean("IS_ENABLED");
if (resultSet.wasNull()) isEnabled = null;
Integer suspendBy = resultSet.getInt("SUSPEND_BY");
if (resultSet.wasNull()) suspendBy = null;
Integer alertId = resultSet.getInt("ALERT_ID");
if (resultSet.wasNull()) alertId = null;
String metricGroupTagsInclusive = resultSet.getString("METRIC_GROUP_TAGS_INCLUSIVE");
if (resultSet.wasNull()) metricGroupTagsInclusive = null;
String metricGroupTagsExclusive = resultSet.getString("METRIC_GROUP_TAGS_EXCLUSIVE");
if (resultSet.wasNull()) metricGroupTagsExclusive = null;
String metricSuspensionRegexes = resultSet.getString("METRIC_SUSPENSION_REGEXES");
if (resultSet.wasNull()) metricSuspensionRegexes = null;
Boolean isOneTime = resultSet.getBoolean("IS_ONE_TIME");
if (resultSet.wasNull()) isOneTime = null;
Boolean isSuspendNotificationOnly = resultSet.getBoolean("IS_SUSPEND_NOTIFICATION_ONLY");
if (resultSet.wasNull()) isSuspendNotificationOnly = null;
Boolean isRecurSunday = resultSet.getBoolean("IS_RECUR_SUNDAY");
if (resultSet.wasNull()) isRecurSunday = null;
Boolean isRecurMonday = resultSet.getBoolean("IS_RECUR_MONDAY");
if (resultSet.wasNull()) isRecurMonday = null;
Boolean isRecurTuesday = resultSet.getBoolean("IS_RECUR_TUESDAY");
if (resultSet.wasNull()) isRecurTuesday = null;
Boolean isRecurWednesday = resultSet.getBoolean("IS_RECUR_WEDNESDAY");
if (resultSet.wasNull()) isRecurWednesday = null;
Boolean isRecurThursday = resultSet.getBoolean("IS_RECUR_THURSDAY");
if (resultSet.wasNull()) isRecurThursday = null;
Boolean isRecurFriday = resultSet.getBoolean("IS_RECUR_FRIDAY");
if (resultSet.wasNull()) isRecurFriday = null;
Boolean isRecurSaturday = resultSet.getBoolean("IS_RECUR_SATURDAY");
if (resultSet.wasNull()) isRecurSaturday = null;
Timestamp startDate = resultSet.getTimestamp("START_DATE");
if (resultSet.wasNull()) startDate = null;
Timestamp startTime = resultSet.getTimestamp("START_TIME");
if (resultSet.wasNull()) startTime = null;
Long duration = resultSet.getLong("DURATION");
if (resultSet.wasNull()) duration = null;
Integer durationTimeUnit = resultSet.getInt("DURATION_TIME_UNIT");
if (resultSet.wasNull()) durationTimeUnit = null;
Timestamp deleteAtTimestamp = resultSet.getTimestamp("DELETE_AT_TIMESTAMP");
if (resultSet.wasNull()) deleteAtTimestamp = null;
Suspension suspension = new Suspension(
id, name, uppercaseName, description, isEnabled, suspendBy, alertId, metricGroupTagsInclusive, metricGroupTagsExclusive, metricSuspensionRegexes,
isOneTime, isSuspendNotificationOnly, isRecurSunday, isRecurMonday, isRecurTuesday, isRecurWednesday, isRecurThursday, isRecurFriday, isRecurSaturday,
startDate, startTime, duration, durationTimeUnit, deleteAtTimestamp);
return suspension;
}
catch (Exception e) {
logger.error(e.toString() + System.lineSeparator() + StackTrace.getStringFromStackTrace(e));
return null;
}
}