Skip to content
Snippets Groups Projects
Commit 1d057156 authored by joeappleton18's avatar joeappleton18
Browse files

finished exercise-2-2-solution

parent cc353308
No related branches found
No related tags found
No related merge requests found
package com.yatl; package com.yatl;
import java.net.URL;
import java.sql.Connection; import java.sql.Connection;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
...@@ -7,44 +7,39 @@ import java.sql.Statement; ...@@ -7,44 +7,39 @@ import java.sql.Statement;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import com.yatl.util.Database;
import com.yatl.model.Todo; import com.yatl.model.Todo;
import com.yatl.util.Database;
public class Main { public class Main {
static List<Todo> todos = new ArrayList<>(); static List<Todo> todos = new ArrayList<>();
public static void main(String[] args) { public static void main(String[] args) {
URL resourceFolderUrl = Main.class.getClassLoader().getResource(""); String databasePath = "src/main/resources/database.db";
String databasePath = resourceFolderUrl.getPath() + "database.db";
String url = "jdbc:sqlite:" + databasePath; String url = "jdbc:sqlite:" + databasePath;
Database database = Database.getInstance(url);
Database db = Database.getInstance(url); Connection conn = database.getConnection();
Connection conn = db.getConnection();
Statement stmt; Statement stmt;
try { try {
stmt = conn.createStatement(); stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM todos"); ResultSet rs = stmt.executeQuery("SELECT * FROM todos");
while (rs.next()) { while (rs.next()) {
todos.add(new Todo(rs.getInt("id"), rs.getString("title"), rs.getBoolean("completed"))); todos.add(new Todo(rs.getInt("id"), rs.getString("title"), rs.getBoolean("completed")));
} }
for(Todo todo: todos) { for (Todo todo : todos) {
System.out.println(todo.toString()); System.out.println(todo.toString());
} }
} catch (SQLException e) { } catch (SQLException e) {
// TODO Auto-generated catch block // TODO Auto-generated catch block
e.printStackTrace(); e.printStackTrace();
} }
} }
} }
...@@ -9,7 +9,6 @@ public class Todo { ...@@ -9,7 +9,6 @@ public class Todo {
public Todo(int id, String title, boolean done) { public Todo(int id, String title, boolean done) {
super();
this.id = id; this.id = id;
this.title = title; this.title = title;
this.done = done; this.done = done;
......
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