Delete useless DummyConnectionWrapper class
This commit is contained in:
parent
a30e86016c
commit
3f19bd2737
@ -1,18 +1,11 @@
|
|||||||
package fr.gasser.daoexample.dao;
|
package fr.gasser.daoexample.dao;
|
||||||
|
|
||||||
import fr.gasser.daoexample.model.Entity;
|
import fr.gasser.daoexample.model.Entity;
|
||||||
import fr.gasser.daoexample.dao.connection.ConnectionWrapper;
|
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public abstract class Dao<T extends Entity> {
|
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 boolean create(T obj);
|
||||||
|
|
||||||
public abstract T find(int id);
|
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.Discipline;
|
||||||
import fr.gasser.daoexample.model.Student;
|
import fr.gasser.daoexample.model.Student;
|
||||||
import fr.gasser.daoexample.model.Teacher;
|
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 {
|
public class DaoFactory implements DaoAbstractFactory {
|
||||||
private static final ConnectionWrapper CONNECTION_WRAPPER = new DummyConnectionWrapper();
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Dao<Student> createStudentDao() {
|
public Dao<Student> createStudentDao() {
|
||||||
return new StudentDao(DaoFactory.CONNECTION_WRAPPER);
|
return new StudentDao();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Dao<Teacher> createTeacherDao() {
|
public Dao<Teacher> createTeacherDao() {
|
||||||
return new TeacherDao(DaoFactory.CONNECTION_WRAPPER);
|
return new TeacherDao();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Dao<Discipline> createDisciplineDao() {
|
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.dao.Dao;
|
||||||
import fr.gasser.daoexample.model.Discipline;
|
import fr.gasser.daoexample.model.Discipline;
|
||||||
import fr.gasser.daoexample.dao.connection.ConnectionWrapper;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
@ -17,10 +16,6 @@ public class DisciplineDao extends Dao<Discipline> {
|
|||||||
new Discipline(2, "Qsdf")
|
new Discipline(2, "Qsdf")
|
||||||
));
|
));
|
||||||
|
|
||||||
public DisciplineDao(ConnectionWrapper connectionWrapper) {
|
|
||||||
super(connectionWrapper);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean create(Discipline obj) {
|
public boolean create(Discipline obj) {
|
||||||
return false;
|
return false;
|
||||||
|
@ -2,7 +2,6 @@ package fr.gasser.daoexample.dao.dummy;
|
|||||||
|
|
||||||
import fr.gasser.daoexample.dao.Dao;
|
import fr.gasser.daoexample.dao.Dao;
|
||||||
import fr.gasser.daoexample.model.Student;
|
import fr.gasser.daoexample.model.Student;
|
||||||
import fr.gasser.daoexample.dao.connection.ConnectionWrapper;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
@ -17,10 +16,6 @@ public class StudentDao extends Dao<Student> {
|
|||||||
new Student(2, "Qsdf", "Jklm"))
|
new Student(2, "Qsdf", "Jklm"))
|
||||||
);
|
);
|
||||||
|
|
||||||
StudentDao(ConnectionWrapper connectionWrapper) {
|
|
||||||
super(connectionWrapper);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean create(Student obj) {
|
public boolean create(Student obj) {
|
||||||
students.add(obj);
|
students.add(obj);
|
||||||
|
@ -2,7 +2,6 @@ package fr.gasser.daoexample.dao.dummy;
|
|||||||
|
|
||||||
import fr.gasser.daoexample.dao.Dao;
|
import fr.gasser.daoexample.dao.Dao;
|
||||||
import fr.gasser.daoexample.model.Teacher;
|
import fr.gasser.daoexample.model.Teacher;
|
||||||
import fr.gasser.daoexample.dao.connection.ConnectionWrapper;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
@ -17,10 +16,6 @@ public class TeacherDao extends Dao<Teacher> {
|
|||||||
new Teacher(2, "Sit", "Amet"))
|
new Teacher(2, "Sit", "Amet"))
|
||||||
);
|
);
|
||||||
|
|
||||||
TeacherDao(ConnectionWrapper connectionWrapper) {
|
|
||||||
super(connectionWrapper);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean create(Teacher obj) {
|
public boolean create(Teacher obj) {
|
||||||
return teachers.add(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.Dao;
|
||||||
import fr.gasser.daoexample.dao.DaoAbstractFactory;
|
import fr.gasser.daoexample.dao.DaoAbstractFactory;
|
||||||
|
import fr.gasser.daoexample.dao.connection.ConnectionWrapper;
|
||||||
import fr.gasser.daoexample.dao.dummy.FactoryType;
|
import fr.gasser.daoexample.dao.dummy.FactoryType;
|
||||||
import fr.gasser.daoexample.model.Student;
|
import fr.gasser.daoexample.model.Student;
|
||||||
import fr.gasser.daoexample.dao.connection.ConnectionWrapper;
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
@ -18,12 +18,13 @@ import java.util.List;
|
|||||||
public class StudentSQLiteDao extends Dao<Student> {
|
public class StudentSQLiteDao extends Dao<Student> {
|
||||||
|
|
||||||
private static final Logger LOGGER = LoggerFactory.getLogger(StudentSQLiteDao.class);
|
private static final Logger LOGGER = LoggerFactory.getLogger(StudentSQLiteDao.class);
|
||||||
|
private final ConnectionWrapper wrapper;
|
||||||
|
|
||||||
StudentSQLiteDao(ConnectionWrapper connectionWrapper) {
|
StudentSQLiteDao(ConnectionWrapper wrapper) {
|
||||||
super(connectionWrapper);
|
this.wrapper = wrapper;
|
||||||
try {
|
try {
|
||||||
connectionWrapper.connect();
|
wrapper.connect();
|
||||||
dropAndCreateTable(connectionWrapper);
|
dropAndCreateTable(wrapper);
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
LOGGER.error("Unable to connect to the database.", e);
|
LOGGER.error("Unable to connect to the database.", e);
|
||||||
}
|
}
|
||||||
@ -44,7 +45,7 @@ public class StudentSQLiteDao extends Dao<Student> {
|
|||||||
@Override
|
@Override
|
||||||
public boolean create(Student obj) {
|
public boolean create(Student obj) {
|
||||||
final String sql = "INSERT INTO students (name, surname) VALUES (?, ?)";
|
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(1, obj.getName());
|
||||||
st.setObject(2, obj.getSurname());
|
st.setObject(2, obj.getSurname());
|
||||||
final int ret = st.executeUpdate();
|
final int ret = st.executeUpdate();
|
||||||
@ -58,7 +59,7 @@ public class StudentSQLiteDao extends Dao<Student> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Student find(int id) {
|
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);
|
st.setObject(1, id);
|
||||||
final ResultSet rs = st.executeQuery();
|
final ResultSet rs = st.executeQuery();
|
||||||
final String name = rs.getString("name");
|
final String name = rs.getString("name");
|
||||||
@ -73,7 +74,7 @@ public class StudentSQLiteDao extends Dao<Student> {
|
|||||||
@Override
|
@Override
|
||||||
public List<Student> findAll() {
|
public List<Student> findAll() {
|
||||||
List<Student> results = new ArrayList<>();
|
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");
|
ResultSet rs = st.executeQuery("select rowid,* from students");
|
||||||
while (rs.next()) {
|
while (rs.next()) {
|
||||||
final int id = rs.getInt("rowid");
|
final int id = rs.getInt("rowid");
|
||||||
@ -89,7 +90,7 @@ public class StudentSQLiteDao extends Dao<Student> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean update(Student obj) {
|
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(1, obj.getName());
|
||||||
st.setString(2, obj.getSurname());
|
st.setString(2, obj.getSurname());
|
||||||
st.setInt(3, obj.getId());
|
st.setInt(3, obj.getId());
|
||||||
@ -104,7 +105,7 @@ public class StudentSQLiteDao extends Dao<Student> {
|
|||||||
@Override
|
@Override
|
||||||
public boolean delete(Student obj) {
|
public boolean delete(Student obj) {
|
||||||
String deleteSQL = "DELETE FROM students WHERE rowid = ?";
|
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.setInt(1, obj.getId());
|
||||||
st.executeUpdate();
|
st.executeUpdate();
|
||||||
return true;
|
return true;
|
||||||
|
Loading…
Reference in New Issue
Block a user