133 lines
4.7 KiB
Java
133 lines
4.7 KiB
Java
package fr.gasser.daoexample.dao;
|
|
|
|
import fr.gasser.daoexample.model.Student;
|
|
import fr.gasser.daoexample.sql.Connection;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import java.sql.PreparedStatement;
|
|
import java.sql.ResultSet;
|
|
import java.sql.SQLException;
|
|
import java.sql.Statement;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
public class StudentSQLiteDao extends Dao<Student> {
|
|
|
|
private static final Logger LOGGER = LoggerFactory.getLogger(StudentSQLiteDao.class);
|
|
|
|
public StudentSQLiteDao(Connection connection) {
|
|
super(connection);
|
|
try {
|
|
connection.connect();
|
|
dropAndCreateTable(connection);
|
|
} catch (SQLException e) {
|
|
LOGGER.error("Unable to connect to the database.", e);
|
|
}
|
|
}
|
|
|
|
private void dropAndCreateTable(Connection connection) throws SQLException {
|
|
try (Statement st = connection.getConnection().createStatement()) {
|
|
st.setQueryTimeout(30); // set timeout to 30 sec.
|
|
st.executeUpdate("DROP TABLE IF EXISTS students");
|
|
// In SQLite, table rows normally have a 64-bit signed integer ROWID
|
|
// which is unique among all rows in the same table.
|
|
// When a new row is inserted into an SQLite table, the ROWID can either be specified as part of the INSERT
|
|
// statement or it can be assigned automatically by the database engine.
|
|
st.executeUpdate("CREATE TABLE students (name TEXT NOT NULL, surname TEXT NOT NULL)");
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public boolean create(Student obj) {
|
|
final String sql = "INSERT INTO students (name, surname) VALUES (?, ?)";
|
|
try (PreparedStatement st = connection.getConnection().prepareStatement(sql)) {
|
|
st.setObject(1, obj.getName());
|
|
st.setObject(2, obj.getSurname());
|
|
final int ret = st.executeUpdate();
|
|
LOGGER.info("" + ret);
|
|
return ret != 0;
|
|
} catch (SQLException e) {
|
|
LOGGER.error("", e);
|
|
}
|
|
return false;
|
|
}
|
|
|
|
@Override
|
|
public Student find(int id) {
|
|
try (PreparedStatement st = connection.getConnection().prepareStatement("SELECT rowid,* FROM students WHERE rowid = ?")) {
|
|
st.setObject(1, id);
|
|
final ResultSet rs = st.executeQuery();
|
|
final String name = rs.getString("name");
|
|
final String surname = rs.getString("surname");
|
|
return new Student(id, name, surname);
|
|
} catch (SQLException e) {
|
|
LOGGER.error("", e);
|
|
}
|
|
return null;
|
|
}
|
|
|
|
@Override
|
|
public List<Student> findAll() {
|
|
List<Student> results = new ArrayList<>();
|
|
try (Statement st = connection.getConnection().createStatement()) {
|
|
ResultSet rs = st.executeQuery("select rowid,* from students");
|
|
while (rs.next()) {
|
|
final int id = rs.getInt("rowid");
|
|
final String name = rs.getString("name");
|
|
final String surname = rs.getString("surname");
|
|
results.add(new Student(id, name, surname));
|
|
}
|
|
} catch (SQLException e) {
|
|
LOGGER.error("", e);
|
|
}
|
|
return results;
|
|
}
|
|
|
|
@Override
|
|
public boolean update(Student obj) {
|
|
try (PreparedStatement st = connection.getConnection().prepareStatement("UPDATE students SET name = ?, surname = ? WHERE rowid = ?")) {
|
|
st.setString(1, obj.getName());
|
|
st.setString(2, obj.getSurname());
|
|
st.setInt(3, obj.getId());
|
|
st.executeUpdate();
|
|
return true;
|
|
} catch (SQLException e) {
|
|
LOGGER.error("", e);
|
|
}
|
|
return false;
|
|
}
|
|
|
|
@Override
|
|
public boolean delete(Student obj) {
|
|
String deleteSQL = "DELETE FROM students WHERE rowid = ?";
|
|
try (PreparedStatement st = connection.getConnection().prepareStatement(deleteSQL)) {
|
|
st.setInt(1, obj.getId());
|
|
st.executeUpdate();
|
|
return true;
|
|
} catch (SQLException e) {
|
|
LOGGER.error("", e);
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Quick testing of CRUD operations
|
|
* @param args
|
|
*/
|
|
public static void main(String[] args) {
|
|
DaoAbstractFactory df = DaoAbstractFactory.createFactory(FactoryType.SqliteDaoFactory);
|
|
final Dao<Student> dao = df.createStudentDao();
|
|
dao.create(new Student(1, "Thibaud", "Gasser"));
|
|
dao.create(new Student(2, "Azerty", "Uiop"));
|
|
final Student s = dao.find(1);
|
|
System.out.println(s);
|
|
s.setName("Foo");
|
|
dao.update(s);
|
|
System.out.println(dao.findAll());
|
|
dao.delete(s);
|
|
System.out.println(dao.findAll());
|
|
}
|
|
}
|