Skip to content

Commit

Permalink
initial
Browse files Browse the repository at this point in the history
  • Loading branch information
suryabahadurkathayat committed Jan 6, 2026
1 parent 391d344 commit 03e933e
Show file tree
Hide file tree
Showing 12 changed files with 455 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,8 @@
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
replay_pid*

.idea
*.idea/

*.DS_Store
18 changes: 18 additions & 0 deletions src/main/java/no/ntnu/idatx1005/demo/MyApp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package no.ntnu.idatx1005.demo;

import no.ntnu.idatx1005.demo.view.MyWindow;

/**
* Use this class to start the application
* @author nilstes
*/
public class MyApp {

/**
* Main method for my application
*/
public static void main(String[] args) throws Exception {
MyWindow window = new MyWindow("The Window");
window.setVisible(true);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package no.ntnu.idatx1005.demo.dao;


import java.sql.*;

public class PostgresDBConnectionProvider {
private static PostgresDBConnectionProvider databaseConnectionProvider;
private static String DB_NAME = "idatt1005-db";
private static String DATABASE_URL = "jdbc:postgresql://localhost:5432/" + DB_NAME;
private static String username = "idatt1005user";
private static String password = "idatt1005password";

public PostgresDBConnectionProvider() {
}

Connection getConnection() {
try {
// Note: In a real application, you should not connect to the database like this. Instead, you should use a connection pool.
// In addition, database should be protected
return DriverManager.getConnection(DATABASE_URL, username, password);
} catch (Exception e) {
throw new RuntimeException(e);
}
}

public static PostgresDBConnectionProvider instance() {
if (databaseConnectionProvider == null) {
databaseConnectionProvider = new PostgresDBConnectionProvider();
return databaseConnectionProvider;
} else {
return databaseConnectionProvider;
}
}

/**
* Closes connections to database, makes sure that resultSets, and statements gets closed properly
* @param connection the connection to be closed
* @param preparedStatement the preparedStatement to be closed
* @param resultSet the resultSet to be closed
*/
static void close(Connection connection, PreparedStatement preparedStatement, ResultSet resultSet) {
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (preparedStatement != null) {
try {
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}

/**
* Method that creates a database if it does not exist.
* Note that you should not use this method in production code. Instead, you should create the database tables via SQL scripts.
* The method creates tables for User.
*
* @throws SQLException if creating the database fails
*/
public static void createDB() throws SQLException {

try (Connection connection = DriverManager.getConnection(DATABASE_URL)) {

// Sql-statements for creating tables
// NOTE: Passwords should never be stored in plain text in a real/production application. They should be hashed and salted.
String createUserTable =
"CREATE TABLE IF NOT EXISTS Users (" +
"userId uuid primary key, " +
"username VARCHAR(255), " +
"password VARCHAR(255)" +
");";
executeStatement(connection, createUserTable);
} catch (SQLException e) {
throw new SQLException("Error creating tables: " + e.getMessage(), e);
}
}

/**
* Method that executes a sql statement.
*
* @param connection connection to the database
* @param sql the sql statement to execute
* @throws SQLException if executing the statement fails
*/
private static void executeStatement(Connection connection, String sql) throws SQLException {
try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
preparedStatement.executeUpdate();
} catch (SQLException e) {
throw new SQLException("Error executing statement: " + e.getMessage(), e);
}
}

}
93 changes: 93 additions & 0 deletions src/main/java/no/ntnu/idatx1005/demo/dao/UserDAO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package no.ntnu.idatx1005.demo.dao;

import no.ntnu.idatx1005.demo.data.User;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
* Data access object for User
*/
public class UserDAO {
private PostgresDBConnectionProvider connectionProvider;

public UserDAO(PostgresDBConnectionProvider connectionProvider) {
this.connectionProvider = connectionProvider;
}

/**
* Returns a List of all registered users
*
* @return List of Users
*/
public List<User> getUsers() {
List<User> users = new ArrayList<>();
PreparedStatement preparedStatement = null;
Connection connection = null;
ResultSet resultSet = null;
try {
connection = connectionProvider.getConnection();
preparedStatement = connection.prepareStatement("SELECT * FROM userinfo");
resultSet = preparedStatement.executeQuery();

User user;
while (resultSet.next()) {
user = new User();
user.setUserid(UUID.fromString(resultSet.getString("userId")));
user.setUsername(resultSet.getString("username"));
users.add(user);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
PostgresDBConnectionProvider.close(connection, preparedStatement, resultSet);
}
return users;
}

/**
* Adds a new user to database with default ID
*
* @param user User object
* @return new User or already registered user
*/
public User addUser(User user) {
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;

// check if user already exists in database
User existingUser = getUsers().stream().allMatch(u -> u.getUsername().equals(user.getUsername())) ? user : null;
if(existingUser != null) {
System.out.println("User already exists");
return existingUser;
}

try {
connection = connectionProvider.getConnection();
User newUser = new User();
preparedStatement = connection.prepareStatement("INSERT INTO userinfo (userId, username, password) VALUES (?, ?, ?)", Statement.RETURN_GENERATED_KEYS);
preparedStatement.setObject(1, user.getUserid(), Types.OTHER);
preparedStatement.setString(2, user.getUsername());
preparedStatement.setString(3, user.getPassword());
int result = preparedStatement.executeUpdate();

if (result == 1) {
newUser.setUsername(user.getUsername());
newUser.setUserid(user.getUserid());
return newUser;
}
} catch (SQLException e) {
e.printStackTrace();

} finally {
PostgresDBConnectionProvider.close(connection, preparedStatement, resultSet);
}

return new User();
}

}

37 changes: 37 additions & 0 deletions src/main/java/no/ntnu/idatx1005/demo/data/MyEntity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package no.ntnu.idatx1005.demo.data;

import java.util.Date;
import java.util.List;

/**
* This is just a simple Java-bean
* @author nilstes
*/
public class MyEntity {
private String id;
private String name;

public MyEntity() {
}

public MyEntity(String id, String name) {
this.id = id;
this.name = name;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}
}
45 changes: 45 additions & 0 deletions src/main/java/no/ntnu/idatx1005/demo/data/User.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package no.ntnu.idatx1005.demo.data;


import java.util.UUID;

/**
* Class for the User object as saved in database
*/
public class User {
private UUID userid;
private String username;
private String password;

public User(){}

public User(UUID userId, String username, String password){
this.userid = userId;
this.username = username;
this.password = password;
}

public UUID getUserid() {
return userid;
}

public void setUserid(UUID userid) {
this.userid = userid;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}
}
46 changes: 46 additions & 0 deletions src/main/java/no/ntnu/idatx1005/demo/repo/MyEntityRepo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package no.ntnu.idatx1005.demo.repo;

import no.ntnu.idatx1005.demo.data.MyEntity;

import java.util.Arrays;
import java.util.List;

/**
* Repository for the MyEntity-entity
*
* @author nilstes
*/
public class MyEntityRepo {

/**
* Get object with given id
*
* @param id the entity id
* @return an instance of MyEntity
*/
public MyEntity getMyEntity(String id) {
// Get connection (maybe use pool?)
// Do some SQL
// Return som real data

return new MyEntity("id", "name");
}

public List<MyEntity> findMyEntities(String someParameter) {
// Get connection (maybe use pool?)
// Do some SQL
// Return som real data

return Arrays.asList(new MyEntity("id1", "name1"), new MyEntity("id2", "name2"));
}

public void addMyEntity(MyEntity obj) {
// Get connection (maybe use pool?)
// Do some SQL
}

public void deleteMyEntity(String id) {
// Get connection (maybe use pool?)
// Do some SQL
}
}
Loading

0 comments on commit 03e933e

Please sign in to comment.