From 4cc19191c0cdf90398ae3b1c63f0bc543e5f1cb4 Mon Sep 17 00:00:00 2001 From: Thibaud Date: Wed, 2 Jan 2019 22:50:07 +0100 Subject: [PATCH] Create SqliteDaoFactory --- persistence/pom.xml | 8 ++++ .../daoexample/dao/DaoAbstractFactory.java | 2 + .../fr/gasser/daoexample/dao/FactoryType.java | 3 +- .../daoexample/dao/SqliteDaoFactory.java | 20 ++++++++ .../daoexample/sql/SQLiteConnection.java | 47 +++++++++++++++++++ 5 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 persistence/src/main/java/fr/gasser/daoexample/dao/SqliteDaoFactory.java create mode 100644 persistence/src/main/java/fr/gasser/daoexample/sql/SQLiteConnection.java diff --git a/persistence/pom.xml b/persistence/pom.xml index 3e6933e..1893e05 100644 --- a/persistence/pom.xml +++ b/persistence/pom.xml @@ -13,5 +13,13 @@ patterns 1.0-SNAPSHOT + + + org.xerial + sqlite-jdbc + 3.25.2 + + + \ No newline at end of file diff --git a/persistence/src/main/java/fr/gasser/daoexample/dao/DaoAbstractFactory.java b/persistence/src/main/java/fr/gasser/daoexample/dao/DaoAbstractFactory.java index ed8759e..ad5b04b 100644 --- a/persistence/src/main/java/fr/gasser/daoexample/dao/DaoAbstractFactory.java +++ b/persistence/src/main/java/fr/gasser/daoexample/dao/DaoAbstractFactory.java @@ -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(); diff --git a/persistence/src/main/java/fr/gasser/daoexample/dao/FactoryType.java b/persistence/src/main/java/fr/gasser/daoexample/dao/FactoryType.java index cc27066..008e26c 100644 --- a/persistence/src/main/java/fr/gasser/daoexample/dao/FactoryType.java +++ b/persistence/src/main/java/fr/gasser/daoexample/dao/FactoryType.java @@ -2,5 +2,6 @@ package fr.gasser.daoexample.dao; public enum FactoryType { DaoFactory, - XmlDaoFactory + XmlDaoFactory, + SqliteDaoFactory } diff --git a/persistence/src/main/java/fr/gasser/daoexample/dao/SqliteDaoFactory.java b/persistence/src/main/java/fr/gasser/daoexample/dao/SqliteDaoFactory.java new file mode 100644 index 0000000..4546b44 --- /dev/null +++ b/persistence/src/main/java/fr/gasser/daoexample/dao/SqliteDaoFactory.java @@ -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; + } +} diff --git a/persistence/src/main/java/fr/gasser/daoexample/sql/SQLiteConnection.java b/persistence/src/main/java/fr/gasser/daoexample/sql/SQLiteConnection.java new file mode 100644 index 0000000..b2f207e --- /dev/null +++ b/persistence/src/main/java/fr/gasser/daoexample/sql/SQLiteConnection.java @@ -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; + } +}