96 lines
2.8 KiB
Java
96 lines
2.8 KiB
Java
package us.tastybento.bskyblock.database.mysql;
|
|
|
|
import java.beans.IntrospectionException;
|
|
import java.beans.PropertyDescriptor;
|
|
import java.lang.reflect.Field;
|
|
import java.lang.reflect.InvocationTargetException;
|
|
import java.lang.reflect.Method;
|
|
import java.sql.Connection;
|
|
import java.sql.PreparedStatement;
|
|
import java.sql.SQLException;
|
|
|
|
import us.tastybento.bskyblock.BSkyBlock;
|
|
import us.tastybento.bskyblock.database.AbstractDatabaseHandler;
|
|
import us.tastybento.bskyblock.database.DatabaseConnecter;
|
|
|
|
/**
|
|
*
|
|
* Class that inserts a <T> into the corresponding database-table.
|
|
*
|
|
* @author tastybento
|
|
*
|
|
* @param <T>
|
|
*/
|
|
public class MySQLDatabaseInserter<T> extends AbstractDatabaseHandler<T> {
|
|
|
|
public MySQLDatabaseInserter(BSkyBlock plugin, Class<T> type,
|
|
DatabaseConnecter databaseConnecter) {
|
|
super(plugin, type, databaseConnecter);
|
|
}
|
|
|
|
@Override
|
|
protected String createQuery() {
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
sb.append("INSERT INTO ");
|
|
sb.append(type.getSimpleName());
|
|
sb.append("(");
|
|
sb.append(super.getColumns(false));
|
|
sb.append(")");
|
|
sb.append(" VALUES (");
|
|
sb.append(super.getColumns(true));
|
|
sb.append(")");
|
|
|
|
return sb.toString();
|
|
}
|
|
|
|
/**
|
|
* Inserts a <T> into the corresponding database-table
|
|
*
|
|
* @param instance <T> that should be inserted into the corresponding database-table
|
|
* @throws SQLException
|
|
* @throws SecurityException
|
|
* @throws IllegalArgumentException
|
|
* @throws InstantiationException
|
|
* @throws IllegalAccessException
|
|
* @throws IntrospectionException
|
|
* @throws InvocationTargetException
|
|
*/
|
|
public void insertObject(T instance) throws SQLException,
|
|
SecurityException, IllegalArgumentException,
|
|
InstantiationException, IllegalAccessException,
|
|
IntrospectionException, InvocationTargetException {
|
|
|
|
Connection connection = null;
|
|
PreparedStatement preparedStatement = null;
|
|
|
|
try {
|
|
connection = databaseConnecter.createConnection();
|
|
preparedStatement = connection.prepareStatement(query);
|
|
|
|
int i = 0;
|
|
|
|
for (Field field : type.getDeclaredFields()) {
|
|
PropertyDescriptor propertyDescriptor = new PropertyDescriptor(
|
|
field.getName(), type);
|
|
|
|
Method method = propertyDescriptor
|
|
.getReadMethod();
|
|
|
|
Object value = method.invoke(instance);
|
|
|
|
preparedStatement.setObject(++i, value);
|
|
}
|
|
|
|
preparedStatement.addBatch();
|
|
|
|
preparedStatement.executeBatch();
|
|
|
|
} finally {
|
|
MySQLDatabaseResourceCloser.close(preparedStatement);
|
|
MySQLDatabaseResourceCloser.close(preparedStatement);
|
|
}
|
|
}
|
|
}
|