Added more try-with-resource to auto-close prepared statement

This commit is contained in:
Tastybento 2018-02-06 21:33:05 -08:00
parent fdc7a62990
commit 2e89a2654f

View File

@ -440,11 +440,12 @@ public class MySQLDatabaseHandler<T> extends AbstractDatabaseHandler<T> {
// Collection // Collection
// The table is cleared for this uniqueId every time the data is stored // The table is cleared for this uniqueId every time the data is stored
String clearTableSql = "DELETE FROM `" + dataObject.getCanonicalName() + "." + field.getName() + "` WHERE uniqueId = ?"; String clearTableSql = "DELETE FROM `" + dataObject.getCanonicalName() + "." + field.getName() + "` WHERE uniqueId = ?";
PreparedStatement collStatement = connection.prepareStatement(clearTableSql); try (PreparedStatement collStatement = connection.prepareStatement(clearTableSql)) {
collStatement.setString(1, uniqueId); collStatement.setString(1, uniqueId);
collStatement.execute(); collStatement.execute();
if (DEBUG) if (DEBUG)
plugin.getLogger().info("DEBUG: collStatement " + collStatement.toString()); plugin.getLogger().info("DEBUG: collStatement " + collStatement.toString());
}
// Insert into the table // Insert into the table
String setSql = "INSERT INTO `" + dataObject.getCanonicalName() + "." + field.getName() + "` (uniqueId, "; String setSql = "INSERT INTO `" + dataObject.getCanonicalName() + "." + field.getName() + "` (uniqueId, ";
// Get the columns we are going to insert, just the names of them // Get the columns we are going to insert, just the names of them
@ -452,60 +453,61 @@ public class MySQLDatabaseHandler<T> extends AbstractDatabaseHandler<T> {
// Get all the ?'s for the columns // Get all the ?'s for the columns
setSql += "VALUES ('" + uniqueId + "'," + getCollectionColumnString(propertyDescriptor.getWriteMethod(), true, false) + ")"; setSql += "VALUES ('" + uniqueId + "'," + getCollectionColumnString(propertyDescriptor.getWriteMethod(), true, false) + ")";
// Prepare the statement // Prepare the statement
collStatement = connection.prepareStatement(setSql); try (PreparedStatement collStatement = connection.prepareStatement(setSql)) {
if (DEBUG) if (DEBUG)
plugin.getLogger().info("DEBUG: collection insert =" + setSql); plugin.getLogger().info("DEBUG: collection insert =" + setSql);
// Do single dimension types (set and list) // Do single dimension types (set and list)
if (propertyDescriptor.getPropertyType().equals(Set.class) || if (propertyDescriptor.getPropertyType().equals(Set.class) ||
propertyDescriptor.getPropertyType().equals(ArrayList.class)) { propertyDescriptor.getPropertyType().equals(ArrayList.class)) {
//plugin.getLogger().info("DEBUG: set class for "); //plugin.getLogger().info("DEBUG: set class for ");
// Loop through the set or list // Loop through the set or list
// Note that we have no idea what type this is // Note that we have no idea what type this is
Collection<?> collection = (Collection<?>)value; Collection<?> collection = (Collection<?>)value;
Iterator<?> it = collection.iterator(); Iterator<?> it = collection.iterator();
while (it.hasNext()) { while (it.hasNext()) {
Object setValue = it.next(); Object setValue = it.next();
//if (setValue instanceof UUID) { //if (setValue instanceof UUID) {
// Serialize everything // Serialize everything
setValue = serialize(setValue, setValue.getClass()); setValue = serialize(setValue, setValue.getClass());
//} //}
// Set the value from ? to whatever it is // Set the value from ? to whatever it is
collStatement.setObject(1, setValue); collStatement.setObject(1, setValue);
if (DEBUG) if (DEBUG)
plugin.getLogger().info("DEBUG: " + collStatement.toString()); plugin.getLogger().info("DEBUG: " + collStatement.toString());
// Execute the SQL in the database // Execute the SQL in the database
collStatement.execute(); collStatement.execute();
} }
} else if (propertyDescriptor.getPropertyType().equals(Map.class) || } else if (propertyDescriptor.getPropertyType().equals(Map.class) ||
propertyDescriptor.getPropertyType().equals(HashMap.class)) { propertyDescriptor.getPropertyType().equals(HashMap.class)) {
// Loop through the map // Loop through the map
Map<?,?> collection = (Map<?,?>)value; Map<?,?> collection = (Map<?,?>)value;
Iterator<?> it = collection.entrySet().iterator(); Iterator<?> it = collection.entrySet().iterator();
while (it.hasNext()) { while (it.hasNext()) {
Entry<?,?> en = (Entry<?, ?>) it.next(); Entry<?,?> en = (Entry<?, ?>) it.next();
if (DEBUG) if (DEBUG)
plugin.getLogger().info("DEBUG: entry ket = " + en.getKey()); plugin.getLogger().info("DEBUG: entry ket = " + en.getKey());
// Get the key and serialize it // Get the key and serialize it
Object key = serialize(en.getKey(), en.getKey().getClass()); Object key = serialize(en.getKey(), en.getKey().getClass());
if (DEBUG) if (DEBUG)
plugin.getLogger().info("DEBUG: key class = " + en.getKey().getClass().getTypeName()); plugin.getLogger().info("DEBUG: key class = " + en.getKey().getClass().getTypeName());
// Get the value and serialize it // Get the value and serialize it
Object mapValue = serialize(en.getValue(), en.getValue().getClass()); Object mapValue = serialize(en.getValue(), en.getValue().getClass());
if (DEBUG) if (DEBUG)
plugin.getLogger().info("DEBUG: mapValue = " + mapValue); plugin.getLogger().info("DEBUG: mapValue = " + mapValue);
// Write the objects into prepared statement // Write the objects into prepared statement
collStatement.setObject(1, key); collStatement.setObject(1, key);
collStatement.setObject(2, mapValue); collStatement.setObject(2, mapValue);
if (DEBUG) if (DEBUG)
plugin.getLogger().info("DEBUG: " + collStatement.toString()); plugin.getLogger().info("DEBUG: " + collStatement.toString());
// Write to database // Write to database
collStatement.execute(); collStatement.execute();
}
} }
// Set value for the main insert. For collections, this is just a dummy value because the real values are in the
// additional table.
value = true;
} }
// Set value for the main insert. For collections, this is just a dummy value because the real values are in the
// additional table.
value = true;
} else { } else {
// If the value is not a collection, it just needs to be serialized to go into the database. // If the value is not a collection, it just needs to be serialized to go into the database.
value = serialize(value, propertyDescriptor.getPropertyType()); value = serialize(value, propertyDescriptor.getPropertyType());