Skip to content
Snippets Groups Projects

Create lab with a prepared statement

2 files
+ 30
9
Compare changes
  • Side-by-side
  • Inline

Files

package nl.tudelft.ewi.tam.repositories;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import nl.tudelft.ewi.tam.JdbcDaoImpl;
import nl.tudelft.ewi.tam.beans.LabSession;
import nl.tudelft.ewi.tam.beans.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.stereotype.Repository;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;
import java.util.ArrayList;
import java.util.Date;
@@ -32,12 +38,6 @@ public class LabRepository {
"INSERT INTO Lab VALUES ()";
/**
* The {@link String} instance used to get the id of the last created lab.
*/
protected static final String GET_ID =
"SELECT last_insert_id() AS lab_id";
/**
* The {@link String} instance used to update the lab session with the given information.
*/
protected static final String UPDATE_SESSIONS =
@@ -94,9 +94,14 @@ public class LabRepository {
* @return the id of the newly created lab
*/
public int createLab() {
jdbcDaoImpl.update(CREATE_LAB, new Object[0], new int[0]);
return jdbcDaoImpl.queryForObject(GET_ID, new Object[0], new int[0],
new BeanPropertyRowMapper<>(LabSession.class)).getLabId();
return jdbcDaoImpl.update(new PreparedStatementCreator() { //Can't lambda since we must suppressFB
@Override
@SuppressWarnings("PMD.AccessorMethodGeneration") //PMD doesn't like that this isn't a lambda
@SuppressFBWarnings("OBL_UNSATISFIED_OBLIGATION_EXCEPTION_EDGE") //False positive; spring closes this for us
public PreparedStatement createPreparedStatement(final Connection connection) throws SQLException {
return connection.prepareStatement(CREATE_LAB, Statement.RETURN_GENERATED_KEYS);
}
}).intValue();
}
/**
Loading