diff --git a/persistence/src/main/java/fr/gasser/daoexample/dao/Dao.java b/persistence/src/main/java/fr/gasser/daoexample/dao/Dao.java index 01fc055..9763ff3 100644 --- a/persistence/src/main/java/fr/gasser/daoexample/dao/Dao.java +++ b/persistence/src/main/java/fr/gasser/daoexample/dao/Dao.java @@ -1,18 +1,11 @@ package fr.gasser.daoexample.dao; import fr.gasser.daoexample.model.Entity; -import fr.gasser.daoexample.dao.connection.ConnectionWrapper; import java.util.List; public abstract class Dao { - protected final ConnectionWrapper connectionWrapper; - - protected Dao(ConnectionWrapper connectionWrapper) { - this.connectionWrapper = connectionWrapper; - } - public abstract boolean create(T obj); public abstract T find(int id); diff --git a/persistence/src/main/java/fr/gasser/daoexample/dao/connection/DummyConnectionWrapper.java b/persistence/src/main/java/fr/gasser/daoexample/dao/connection/DummyConnectionWrapper.java deleted file mode 100644 index 0590415..0000000 --- a/persistence/src/main/java/fr/gasser/daoexample/dao/connection/DummyConnectionWrapper.java +++ /dev/null @@ -1,18 +0,0 @@ -package fr.gasser.daoexample.dao.connection; - -public class DummyConnectionWrapper implements ConnectionWrapper { - @Override - public void connect() { - - } - - @Override - public void close() { - - } - - @Override - public java.sql.Connection getConnection() { - return null; - } -} diff --git a/persistence/src/main/java/fr/gasser/daoexample/dao/dummy/DaoFactory.java b/persistence/src/main/java/fr/gasser/daoexample/dao/dummy/DaoFactory.java index 8dd21e7..12a9966 100644 --- a/persistence/src/main/java/fr/gasser/daoexample/dao/dummy/DaoFactory.java +++ b/persistence/src/main/java/fr/gasser/daoexample/dao/dummy/DaoFactory.java @@ -5,24 +5,21 @@ import fr.gasser.daoexample.dao.DaoAbstractFactory; import fr.gasser.daoexample.model.Discipline; import fr.gasser.daoexample.model.Student; import fr.gasser.daoexample.model.Teacher; -import fr.gasser.daoexample.dao.connection.ConnectionWrapper; -import fr.gasser.daoexample.dao.connection.DummyConnectionWrapper; public class DaoFactory implements DaoAbstractFactory { - private static final ConnectionWrapper CONNECTION_WRAPPER = new DummyConnectionWrapper(); @Override public Dao createStudentDao() { - return new StudentDao(DaoFactory.CONNECTION_WRAPPER); + return new StudentDao(); } @Override public Dao createTeacherDao() { - return new TeacherDao(DaoFactory.CONNECTION_WRAPPER); + return new TeacherDao(); } @Override public Dao createDisciplineDao() { - return new DisciplineDao(DaoFactory.CONNECTION_WRAPPER); + return new DisciplineDao(); } } diff --git a/persistence/src/main/java/fr/gasser/daoexample/dao/dummy/DisciplineDao.java b/persistence/src/main/java/fr/gasser/daoexample/dao/dummy/DisciplineDao.java index af85000..13846ce 100644 --- a/persistence/src/main/java/fr/gasser/daoexample/dao/dummy/DisciplineDao.java +++ b/persistence/src/main/java/fr/gasser/daoexample/dao/dummy/DisciplineDao.java @@ -2,7 +2,6 @@ package fr.gasser.daoexample.dao.dummy; import fr.gasser.daoexample.dao.Dao; import fr.gasser.daoexample.model.Discipline; -import fr.gasser.daoexample.dao.connection.ConnectionWrapper; import java.util.ArrayList; import java.util.Arrays; @@ -17,10 +16,6 @@ public class DisciplineDao extends Dao { new Discipline(2, "Qsdf") )); - public DisciplineDao(ConnectionWrapper connectionWrapper) { - super(connectionWrapper); - } - @Override public boolean create(Discipline obj) { return false; diff --git a/persistence/src/main/java/fr/gasser/daoexample/dao/dummy/StudentDao.java b/persistence/src/main/java/fr/gasser/daoexample/dao/dummy/StudentDao.java index 321d559..c0a6601 100644 --- a/persistence/src/main/java/fr/gasser/daoexample/dao/dummy/StudentDao.java +++ b/persistence/src/main/java/fr/gasser/daoexample/dao/dummy/StudentDao.java @@ -2,7 +2,6 @@ package fr.gasser.daoexample.dao.dummy; import fr.gasser.daoexample.dao.Dao; import fr.gasser.daoexample.model.Student; -import fr.gasser.daoexample.dao.connection.ConnectionWrapper; import java.util.ArrayList; import java.util.Arrays; @@ -17,10 +16,6 @@ public class StudentDao extends Dao { new Student(2, "Qsdf", "Jklm")) ); - StudentDao(ConnectionWrapper connectionWrapper) { - super(connectionWrapper); - } - @Override public boolean create(Student obj) { students.add(obj); diff --git a/persistence/src/main/java/fr/gasser/daoexample/dao/dummy/TeacherDao.java b/persistence/src/main/java/fr/gasser/daoexample/dao/dummy/TeacherDao.java index 97948da..a4796c1 100644 --- a/persistence/src/main/java/fr/gasser/daoexample/dao/dummy/TeacherDao.java +++ b/persistence/src/main/java/fr/gasser/daoexample/dao/dummy/TeacherDao.java @@ -2,7 +2,6 @@ package fr.gasser.daoexample.dao.dummy; import fr.gasser.daoexample.dao.Dao; import fr.gasser.daoexample.model.Teacher; -import fr.gasser.daoexample.dao.connection.ConnectionWrapper; import java.util.ArrayList; import java.util.Arrays; @@ -17,10 +16,6 @@ public class TeacherDao extends Dao { new Teacher(2, "Sit", "Amet")) ); - TeacherDao(ConnectionWrapper connectionWrapper) { - super(connectionWrapper); - } - @Override public boolean create(Teacher obj) { return teachers.add(obj); diff --git a/persistence/src/main/java/fr/gasser/daoexample/dao/sqlite/StudentSQLiteDao.java b/persistence/src/main/java/fr/gasser/daoexample/dao/sqlite/StudentSQLiteDao.java index 1478ec8..077b957 100644 --- a/persistence/src/main/java/fr/gasser/daoexample/dao/sqlite/StudentSQLiteDao.java +++ b/persistence/src/main/java/fr/gasser/daoexample/dao/sqlite/StudentSQLiteDao.java @@ -2,9 +2,9 @@ package fr.gasser.daoexample.dao.sqlite; import fr.gasser.daoexample.dao.Dao; import fr.gasser.daoexample.dao.DaoAbstractFactory; +import fr.gasser.daoexample.dao.connection.ConnectionWrapper; import fr.gasser.daoexample.dao.dummy.FactoryType; import fr.gasser.daoexample.model.Student; -import fr.gasser.daoexample.dao.connection.ConnectionWrapper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -18,12 +18,13 @@ import java.util.List; public class StudentSQLiteDao extends Dao { private static final Logger LOGGER = LoggerFactory.getLogger(StudentSQLiteDao.class); + private final ConnectionWrapper wrapper; - StudentSQLiteDao(ConnectionWrapper connectionWrapper) { - super(connectionWrapper); + StudentSQLiteDao(ConnectionWrapper wrapper) { + this.wrapper = wrapper; try { - connectionWrapper.connect(); - dropAndCreateTable(connectionWrapper); + wrapper.connect(); + dropAndCreateTable(wrapper); } catch (SQLException e) { LOGGER.error("Unable to connect to the database.", e); } @@ -44,7 +45,7 @@ public class StudentSQLiteDao extends Dao { @Override public boolean create(Student obj) { final String sql = "INSERT INTO students (name, surname) VALUES (?, ?)"; - try (PreparedStatement st = connectionWrapper.getConnection().prepareStatement(sql)) { + try (PreparedStatement st = wrapper.getConnection().prepareStatement(sql)) { st.setObject(1, obj.getName()); st.setObject(2, obj.getSurname()); final int ret = st.executeUpdate(); @@ -58,7 +59,7 @@ public class StudentSQLiteDao extends Dao { @Override public Student find(int id) { - try (PreparedStatement st = connectionWrapper.getConnection().prepareStatement("SELECT rowid,* FROM students WHERE rowid = ?")) { + try (PreparedStatement st = wrapper.getConnection().prepareStatement("SELECT rowid,* FROM students WHERE rowid = ?")) { st.setObject(1, id); final ResultSet rs = st.executeQuery(); final String name = rs.getString("name"); @@ -73,7 +74,7 @@ public class StudentSQLiteDao extends Dao { @Override public List findAll() { List results = new ArrayList<>(); - try (Statement st = connectionWrapper.getConnection().createStatement()) { + try (Statement st = wrapper.getConnection().createStatement()) { ResultSet rs = st.executeQuery("select rowid,* from students"); while (rs.next()) { final int id = rs.getInt("rowid"); @@ -89,7 +90,7 @@ public class StudentSQLiteDao extends Dao { @Override public boolean update(Student obj) { - try (PreparedStatement st = connectionWrapper.getConnection().prepareStatement("UPDATE students SET name = ?, surname = ? WHERE rowid = ?")) { + try (PreparedStatement st = wrapper.getConnection().prepareStatement("UPDATE students SET name = ?, surname = ? WHERE rowid = ?")) { st.setString(1, obj.getName()); st.setString(2, obj.getSurname()); st.setInt(3, obj.getId()); @@ -104,7 +105,7 @@ public class StudentSQLiteDao extends Dao { @Override public boolean delete(Student obj) { String deleteSQL = "DELETE FROM students WHERE rowid = ?"; - try (PreparedStatement st = connectionWrapper.getConnection().prepareStatement(deleteSQL)) { + try (PreparedStatement st = wrapper.getConnection().prepareStatement(deleteSQL)) { st.setInt(1, obj.getId()); st.executeUpdate(); return true;