Class SqlConnector

java.lang.Object
edu.ie3.datamodel.io.connectors.SqlConnector
All Implemented Interfaces:
DataConnector

public class SqlConnector extends Object implements DataConnector
Implements a DataConnector for a native SQL connection to a relational database. It was implemented with a PostgreSQL database in mind, so there might be dialect issues if used with other databases.
  • Field Details

    • log

      public static final org.slf4j.Logger log
  • Constructor Details

    • SqlConnector

      public SqlConnector(String jdbcUrl, String userName, String password)
      Initializes a SqlConnector with the given JDBC url, username, password and time util
      Parameters:
      jdbcUrl - the JDBC url, should start with "jdbc:postgresql://" and contain the database name
      userName - Name of the role used for authentication
      password - Password for the role
  • Method Details

    • executeQuery

      public ResultSet executeQuery(Statement stmt, String query) throws SQLException
      Executes the given query. For update queries please use executeUpdate(String).
      Parameters:
      stmt - the created statement
      query - the query to execute
      Returns:
      the query result
      Throws:
      SQLException - if the execution fails
    • executeUpdate

      public int executeUpdate(String updateQuery)
      Executes an update query
      Parameters:
      updateQuery - the query to execute
      Returns:
      The number of updates or a negative number if the execution failed
    • getConnection

      public Connection getConnection() throws SQLException
      Establishes and returns a database connection. If a connection has not been established yet, a new one is created.
      Returns:
      the connection object
      Throws:
      SQLException - if the connection could not be established
    • getConnection

      public Connection getConnection(boolean reuseConnection) throws SQLException
      Establishes and returns a database connection
      Parameters:
      reuseConnection - should the connection be used again, if it is still valid? If not, a new connection will be established
      Returns:
      the connection object
      Throws:
      SQLException - if the connection could not be established
    • shutdown

      public void shutdown()
      Specified by:
      shutdown in interface DataConnector
    • extractFieldMaps

      public List<Map<String,String>> extractFieldMaps(ResultSet rs)
      Extracts all field to value maps from the ResultSet, one for each row
      Parameters:
      rs - the ResultSet to use
      Returns:
      a list of field maps
    • extractFieldMap

      public Map<String,String> extractFieldMap(ResultSet rs)
      Extracts only the current row of the ResultSet into a field to value map
      Parameters:
      rs - the ResultSet to use
      Returns:
      the field map for the current row