import java.io.IOException;
import java.sql.*;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class DatabaseUtil {
private static final String DB_URL = "jdbc:sqlite:globe_guru.db";
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(DB_URL);
}
public static void initializeDatabase() throws SQLException {
try (Connection conn = getConnection();
PreparedStatement stmt1 = conn.prepareStatement(
"CREATE TABLE IF NOT EXISTS users (" +
"id INTEGER PRIMARY KEY AUTOINCREMENT, " +
"username TEXT NOT NULL UNIQUE, " +
"email TEXT NOT NULL UNIQUE, " +
"password TEXT, " +
"isAdmin BOOLEAN NOT NULL DEFAULT FALSE)"
);
PreparedStatement stmt2 = conn.prepareStatement(
"CREATE TABLE IF NOT EXISTS options (" +
"id INTEGER PRIMARY KEY AUTOINCREMENT, " +
"link TEXT, " +
"imgSrc TEXT, " +
"hotelName TEXT, " +
"country TEXT, " +
"dateRange TEXT, " +
"numberOfPeople INTEGER, " +
"isPriceChanged BOOLEAN DEFAULT 0, " +
"newPrice REAL DEFAULT 0)"
);
PreparedStatement stmt3 = conn.prepareStatement(
"CREATE TABLE IF NOT EXISTS savedOptions (" +
"userId INTEGER," +
"detailId INTEGER," +
"FOREIGN KEY(userId) REFERENCES users(id)," +
"FOREIGN KEY(detailId) REFERENCES optionDetails(id)," +
"UNIQUE(userId, detailId))"
);
PreparedStatement stmt4 = conn.prepareStatement(
"CREATE TABLE IF NOT EXISTS optionDetails (" +
"id INTEGER PRIMARY KEY AUTOINCREMENT, " +
"optionId INTEGER NOT NULL, " +
"type TEXT, " +
"board TEXT, " +
"amenities TEXT, " +
"price REAL," +
"FOREIGN KEY(optionId) REFERENCES options(id))"
)) {
stmt1.executeUpdate();
stmt2.executeUpdate();
stmt3.executeUpdate();
stmt4.executeUpdate();
}
}
public static boolean registerUser(String username, String email, String password) throws SQLException {
String sql = "INSERT INTO users (username, email, password) VALUES (?, ?, ?)";
try (Connection conn = getConnection();
PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setString(1, username);
stmt.setString(2, email);
stmt.setString(3, password);
return stmt.executeUpdate() > 0;
}
}
public static boolean authenticateUser(String email, String password) throws SQLException {
String sql = "SELECT password FROM users WHERE email = ?";
try (Connection conn = getConnection();
PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setString(1, email);
try (ResultSet rs = stmt.executeQuery()) {
if (rs.next()) {
String storedPassword = rs.getString("password");
if (password == null) {
// Google login
return storedPassword == null;
}
return password.equals(storedPassword);
}
}
}
return false;
}
public static boolean deleteUser(int userId) throws SQLException {
String selectSql = "SELECT userId FROM users WHERE userId = ?";
String deleteSql = "DELETE FROM users WHERE userId = ?";
String deleteFavoritesSql = "DELETE FROM savedOptions WHERE userId = ?";
try (Connection conn = getConnection();
PreparedStatement selectStmt = conn.prepareStatement(selectSql);
PreparedStatement deleteStmt = conn.prepareStatement(deleteSql);
PreparedStatement deleteFavoritesStmt = conn.prepareStatement(deleteFavoritesSql)) {
selectStmt.setInt(1, userId);
try (ResultSet rs = selectStmt.executeQuery()) {
if (rs.next()) {
deleteStmt.setInt(1, userId);
int rowsAffected = deleteStmt.executeUpdate();
deleteFavoritesStmt.setInt(1, userId);
deleteFavoritesStmt.executeUpdate();
return rowsAffected > 0;
} else {
return false;
}
}
}
}
public static boolean userExists(String email) throws SQLException {
String query = "SELECT COUNT(*) FROM users WHERE email = ?";
try (Connection connection = getConnection();
PreparedStatement statement = connection.prepareStatement(query)) {
statement.setString(1, email);
ResultSet resultSet = statement.executeQuery();
if (resultSet.next()) {
return resultSet.getInt(1) > 0;
}
}
return false;
}
public static boolean isAdmin(String email) throws SQLException {
String selectSql = "SELECT isAdmin FROM users WHERE email = ?";
try (Connection conn = getConnection();
PreparedStatement stmt = conn.prepareStatement(selectSql)) {
stmt.setString(1, email);
try (ResultSet rs = stmt.executeQuery()) {
if (rs.next()) {
return rs.getBoolean("isAdmin");
}
}
}
return false;
}
public static List