Skip to content
Snippets Groups Projects
Commit 13a1ba83 authored by joeappleton18's avatar joeappleton18
Browse files

exercise-2-3-solution finished

parent 49a91bae
No related branches found
No related tags found
No related merge requests found
......@@ -11,5 +11,5 @@ org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=ignore
org.eclipse.jdt.core.compiler.release=disabled
org.eclipse.jdt.core.compiler.release=enabled
org.eclipse.jdt.core.compiler.source=1.7
package com.yatl;
import java.net.URL;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import com.yatl.util.Database;
import com.yatl.util.Seeder;
import com.yatl.model.Todo;
import com.yatl.dao.TodoDao;
import com.yatl.model.Todo;
public class Main {
static List<Todo> todos = new ArrayList<>();
public static void main(String[] args) {
new Seeder();
URL resourceFolderUrl = Main.class.getClassLoader().getResource("");
String databasePath = resourceFolderUrl.getPath() + "database.db";
String url = "jdbc:sqlite:" + databasePath;
Database db = Database.getInstance(url);
Connection conn = db.getConnection();
Statement stmt;
Database.getInstance(url);
try {
stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM todos");
while (rs.next()) {
todos.add(new Todo(rs.getInt("id"), rs.getString("title"), rs.getBoolean("completed")));
System.out.println(todos.toString());
}
TodoDao tododao = new TodoDao();
List<Todo> todos = tododao.getAll();
for (Todo todo : todos) {
System.out.println(todo.toString());
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package com.yatl.dao;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import java.util.ArrayList;
import com.yatl.model.Todo;
import com.yatl.util.Database;
public class TodoDao {
private final Connection conn;
public TodoDao() {
Database database = Database.getInstance();
conn = database.getConnection();
}
public List<Todo> getAll() throws SQLException {
List<Todo> todos = new ArrayList<>();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM todos");
while (rs.next()) {
todos.add(new Todo(rs.getInt("id"), rs.getString("title"), rs.getBoolean("completed")));
}
return todos;
}
}
package com.yatl.dao;
\ No newline at end of file
......@@ -28,6 +28,14 @@ public class Database {
}
public static Database getInstance() {
if (instance == null) {
throw new IllegalStateException("Database instance not set");
}
return instance;
}
public Connection getConnection() {
return connection;
}
......
No preview for this file type
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment