Skip to content

Chinthakas16218080 sem2 #1

Merged
merged 2 commits into from
Nov 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions .idea/jarRepositories.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

78 changes: 78 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.fitlife</groupId>
<artifactId>fitlife-smart-fitness-tracker</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>

<name>FitLife Smart Fitness Tracker</name>

<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<!-- Servlet API for Tomcat 9 (javax namespace) -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>

<!-- JSP + JSTL -->
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>jstl-api</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>

<!-- MySQL Connector (you can change to MariaDB or others) -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.33</version>
</dependency>

<!-- WEKA (stable) -->
<dependency>
<groupId>nz.ac.waikato.cms.weka</groupId>
<artifactId>weka-stable</artifactId>
<version>3.8.0</version>
</dependency>
</dependencies>

<build>
<finalName>fitlife-smart-fitness-tracker</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<configuration>
<source>17</source>
<target>17</target>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.4.0</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
</project>
24 changes: 24 additions & 0 deletions src/main/java/com/fitlife/config/DBConnection.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.fitlife.config;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DBConnection {

private static final String URL = "jdbc:mysql://localhost:3306/fitlife_db";
private static final String USER = "root";
private static final String PASSWORD = "password";

static {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}

public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(URL, USER, PASSWORD);
}
}
84 changes: 84 additions & 0 deletions src/main/java/com/fitlife/dao/UserDAO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package com.fitlife.dao;

import com.fitlife.config.DBConnection;
import com.fitlife.model.User;

import java.sql.*;
import java.time.LocalDateTime;

public class UserDAO {

public User findByUsername(String username) throws SQLException {
// IMPORTANT: use password_hash (NOT password)
String sql = "SELECT id, username, password_hash, email, created_at FROM users WHERE username = ?";

try (Connection conn = DBConnection.getConnection();
PreparedStatement ps = conn.prepareStatement(sql)) {

ps.setString(1, username);
try (ResultSet rs = ps.executeQuery()) {
if (rs.next()) {
return mapRow(rs);
}
}
}
return null;
}

public boolean usernameExists(String username) throws SQLException {
return findByUsername(username) != null;
}

public User createUser(String username, String rawPassword, String email) throws SQLException {
// IMPORTANT: insert into password_hash (NOT password)
String sql = "INSERT INTO users (username, password_hash, email) VALUES (?, ?, ?)";

try (Connection conn = DBConnection.getConnection();
PreparedStatement ps = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) {

ps.setString(1, username);
ps.setString(2, rawPassword); // for coursework, plain text is fine
ps.setString(3, email);
ps.executeUpdate();

try (ResultSet keys = ps.getGeneratedKeys()) {
if (keys.next()) {
int id = keys.getInt(1);
User u = new User();
u.setId(id);
u.setUsername(username);
u.setPasswordHash(rawPassword);
u.setEmail(email);
u.setCreatedAt(LocalDateTime.now());
return u;
}
}
}
return null;
}

public User validateLogin(String username, String rawPassword) throws SQLException {
User u = findByUsername(username);
if (u == null) return null;

if (u.getPasswordHash() != null && u.getPasswordHash().equals(rawPassword)) {
return u;
}
return null;
}

private User mapRow(ResultSet rs) throws SQLException {
User u = new User();
u.setId(rs.getInt("id"));
u.setUsername(rs.getString("username"));
// IMPORTANT: map password_hash column → passwordHash field
u.setPasswordHash(rs.getString("password_hash"));
u.setEmail(rs.getString("email"));

Timestamp ts = rs.getTimestamp("created_at");
if (ts != null) {
u.setCreatedAt(ts.toLocalDateTime());
}
return u;
}
}
Loading