Create SqliteDaoFactory
This commit is contained in:
parent
2013e2e517
commit
4cc19191c0
@ -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>
|
@ -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();
|
||||
|
@ -2,5 +2,6 @@ package fr.gasser.daoexample.dao;
|
||||
|
||||
public enum FactoryType {
|
||||
DaoFactory,
|
||||
XmlDaoFactory
|
||||
XmlDaoFactory,
|
||||
SqliteDaoFactory
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user