Created
May 16, 2016 17:07
-
-
Save dante-byte/958c72d97ac87257d640550e526a7268 to your computer and use it in GitHub Desktop.
ToDoDatabase
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sample; | |
import org.h2.tools.Server; | |
import java.sql.*; | |
import java.util.ArrayList; | |
/** | |
* | |
*/ | |
public class ToDoDatabase { | |
public final static String DB_URL = "jdbc:h2:./main"; | |
public void init() throws SQLException { | |
Server.createWebServer().start(); | |
Connection conn = DriverManager.getConnection(DB_URL); | |
Statement stmt = conn.createStatement(); | |
stmt.execute("CREATE TABLE IF NOT EXISTS todos (id IDENTITY, text VARCHAR, is_done BOOLEAN, user_id INT)"); | |
stmt.execute("CREATE TABLE IF NOT EXISTS users (id IDENTITY, username VARCHAR, fullname VARCHAR)"); | |
} | |
public void insertToDo(Connection conn, String text, int userID) throws SQLException { | |
PreparedStatement stmt = conn.prepareStatement("INSERT INTO todos VALUES (NULL, ?, false, ?)"); | |
stmt.setString(1, text); | |
stmt.setInt(2, userID); | |
stmt.execute(); | |
} | |
public void deleteToDo(Connection conn, String text) throws SQLException { | |
PreparedStatement stmt = conn.prepareStatement("DELETE FROM todos where text = ?"); | |
stmt.setString(1, text); | |
stmt.execute(); | |
} | |
public static ArrayList<ToDoItem> selectToDos(Connection conn) throws SQLException { | |
ArrayList<ToDoItem> items = new ArrayList<>(); | |
Statement stmt = conn.createStatement(); | |
ResultSet results = stmt.executeQuery("SELECT * FROM todos"); | |
while (results.next()) { | |
int id = results.getInt("id"); | |
String text = results.getString("text"); | |
boolean isDone = results.getBoolean("is_done"); | |
items.add(new ToDoItem(id, text, isDone)); | |
} | |
return items; | |
} | |
public static void toggleToDo(Connection conn, int id) throws SQLException { | |
PreparedStatement stmt = conn.prepareStatement("UPDATE todos SET is_done = NOT is_done WHERE id = ?"); | |
stmt.setInt(1, id); | |
stmt.execute(); | |
} | |
public int insertUser(Connection conn, String username, String fullname) throws SQLException { | |
// PreparedStatement stmt = conn.prepareStatement("INSERT INTO users VALUES (NULL, ?, ?)"); | |
// stmt.setString(1, username); | |
// stmt.setString(2, fullname); | |
// stmt.execute(); | |
// return 0; | |
PreparedStatement stmt = conn.prepareStatement("INSERT INTO users VALUES (NULL, ?, ?)"); | |
stmt.setString(1, username); | |
stmt.setString(2, fullname); | |
stmt.execute(); | |
stmt = conn.prepareStatement("SELECT * FROM users where username = ?"); | |
stmt.setString(1, username); | |
ResultSet results = stmt.executeQuery(); | |
results.next(); | |
return results.getInt("id"); | |
} | |
public void deleteUser(Connection conn, String username) throws SQLException { | |
PreparedStatement stmt = conn.prepareStatement("DELETE FROM users where username = ?"); | |
stmt.setString(1, username); | |
stmt.execute(); | |
} | |
public ArrayList<ToDoItem> selectToDosForUser(Connection conn, int userID) throws SQLException { | |
ArrayList<ToDoItem> items = new ArrayList<>(); | |
PreparedStatement stmt = conn.prepareStatement("SELECT * FROM todos " + | |
"INNER JOIN users ON todos.user_id = users.id " + | |
"WHERE users.id = ?"); | |
stmt.setInt(1, userID); | |
ResultSet results = stmt.executeQuery(); | |
//same as selectToDos | |
while (results.next()) { | |
int id = results.getInt("id"); | |
String text = results.getString("text"); | |
boolean isDone = results.getBoolean("is_done"); | |
items.add(new ToDoItem(id, text, isDone)); | |
} | |
return items; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment