Delete useless DummyConnectionWrapper class
This commit is contained in:
parent
a30e86016c
commit
3f19bd2737
@ -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<T extends Entity> {
|
||||
|
||||
protected final ConnectionWrapper connectionWrapper;
|
||||
|
||||
protected Dao(ConnectionWrapper connectionWrapper) {
|
||||
this.connectionWrapper = connectionWrapper;
|
||||
}
|
||||
|
||||
public abstract boolean create(T obj);
|
||||
|
||||
public abstract T find(int id);
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
@ -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<Student> createStudentDao() {
|
||||
return new StudentDao(DaoFactory.CONNECTION_WRAPPER);
|
||||
return new StudentDao();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Dao<Teacher> createTeacherDao() {
|
||||
return new TeacherDao(DaoFactory.CONNECTION_WRAPPER);
|
||||
return new TeacherDao();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Dao<Discipline> createDisciplineDao() {
|
||||
return new DisciplineDao(DaoFactory.CONNECTION_WRAPPER);
|
||||
return new DisciplineDao();
|
||||
}
|
||||
}
|
||||
|
@ -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<Discipline> {
|
||||
new Discipline(2, "Qsdf")
|
||||
));
|
||||
|
||||
public DisciplineDao(ConnectionWrapper connectionWrapper) {
|
||||
super(connectionWrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean create(Discipline obj) {
|
||||
return false;
|
||||
|
@ -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<Student> {
|
||||
new Student(2, "Qsdf", "Jklm"))
|
||||
);
|
||||
|
||||
StudentDao(ConnectionWrapper connectionWrapper) {
|
||||
super(connectionWrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean create(Student obj) {
|
||||
students.add(obj);
|
||||
|
@ -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<Teacher> {
|
||||
new Teacher(2, "Sit", "Amet"))
|
||||
);
|
||||
|
||||
TeacherDao(ConnectionWrapper connectionWrapper) {
|
||||
super(connectionWrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean create(Teacher obj) {
|
||||
return teachers.add(obj);
|
||||
|
@ -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<Student> {
|
||||
|
||||
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<Student> {
|
||||
@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<Student> {
|
||||
|
||||
@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<Student> {
|
||||
@Override
|
||||
public List<Student> findAll() {
|
||||
List<Student> 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<Student> {
|
||||
|
||||
@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<Student> {
|
||||
@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;
|
||||
|
Loading…
Reference in New Issue
Block a user