Create SqliteDaoFactory

This commit is contained in:
Thibaud Gasser 2019-01-02 22:50:07 +01:00
parent 2013e2e517
commit 4cc19191c0
5 changed files with 79 additions and 1 deletions

View File

@ -13,5 +13,13 @@
<artifactId>patterns</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.25.2</version>
</dependency>
</dependencies>
</project>

View File

@ -6,6 +6,8 @@ public interface DaoAbstractFactory {
switch (type) {
case XmlDaoFactory:
return new XmlDaoFactory();
case SqliteDaoFactory:
return new SqliteDaoFactory();
default:
case DaoFactory:
return new DaoFactory();

View File

@ -2,5 +2,6 @@ package fr.gasser.daoexample.dao;
public enum FactoryType {
DaoFactory,
XmlDaoFactory
XmlDaoFactory,
SqliteDaoFactory
}

View File

@ -0,0 +1,20 @@
package fr.gasser.daoexample.dao;
public class SqliteDaoFactory implements DaoAbstractFactory {
@Override
public StudentDao createStudentDao() {
return null;
}
@Override
public TeacherDao createTeacherDao() {
return null;
}
@Override
public DisciplineDao createDisciplineDao() {
return null;
}
}

View File

@ -0,0 +1,47 @@
package fr.gasser.daoexample.sql;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.sql.DriverManager;
import java.sql.SQLException;
public class SQLiteConnection implements Connection {
private static final Logger LOGGER = LoggerFactory.getLogger(SQLiteConnection.class);
private String dbpath;
private java.sql.Connection connection = null;
public SQLiteConnection(String dBPath) {
dbpath = dBPath;
}
public SQLiteConnection() {
this(":memory:");
}
@Override
public void connect() {
try {
Class.forName("org.sqlite.JDBC");
connection = DriverManager.getConnection("jdbc:sqlite:" + dbpath);
LOGGER.info("Connected to {} successfully.", dbpath);
} catch (ClassNotFoundException | SQLException e) {
LOGGER.error("Unable to connect to the database.", e);
}
}
@Override
public void close() {
try {
connection.close();
} catch (SQLException e) {
LOGGER.error("Unable to close the connection.", e);
}
}
@Override
public java.sql.Connection getConnection() {
return connection;
}
}