Package edu.ie3.datamodel.io.connectors
Class SqlConnector
java.lang.Object
edu.ie3.datamodel.io.connectors.SqlConnector
- All Implemented Interfaces:
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 Summary
-
Constructor Summary
ConstructorDescriptionSqlConnector
(String jdbcUrl, String userName, String password) Initializes a SqlConnector with the given JDBC url, username, password and time util -
Method Summary
Modifier and TypeMethodDescriptionexecuteQuery
(Statement stmt, String query) Executes the given query.int
executeUpdate
(String updateQuery) Executes an update queryExtracts only the current row of the ResultSet into a field to value mapExtracts all field to value maps from the ResultSet, one for each rowEstablishes and returns a database connection.getConnection
(boolean reuseConnection) Establishes and returns a database connectionvoid
shutdown()
-
Field Details
-
log
public static final org.slf4j.Logger log
-
-
Constructor Details
-
SqlConnector
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 nameuserName
- Name of the role used for authenticationpassword
- Password for the role
-
-
Method Details
-
executeQuery
Executes the given query. For update queries please useexecuteUpdate(String)
.- Parameters:
stmt
- the created statementquery
- the query to execute- Returns:
- the query result
- Throws:
SQLException
- if the execution fails
-
executeUpdate
Executes an update query- Parameters:
updateQuery
- the query to execute- Returns:
- The number of updates or a negative number if the execution failed
-
getConnection
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
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 interfaceDataConnector
-
extractFieldMaps
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
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
-