diff --git a/docker-compose.yaml b/docker-compose.yaml index dee12f0..4623ad6 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -2,7 +2,7 @@ version: "3.7" services: postgres: - image: postgres:15.1 + image: postgres:latest restart: always environment: POSTGRES_DB: idatt1005-db @@ -11,8 +11,7 @@ services: ports: - 5432:5432 volumes: - - db:/var/lib/postgresql/data + - db_data:/var/lib/postgresql volumes: - data1-1: - db: { } + db_data: diff --git a/src/main/java/no/ntnu/idatx1005/demo/dbexample/SimpleJdbcExample.java b/src/main/java/no/ntnu/idatx1005/demo/dbexample/SimpleJdbcExample.java new file mode 100644 index 0000000..e193b13 --- /dev/null +++ b/src/main/java/no/ntnu/idatx1005/demo/dbexample/SimpleJdbcExample.java @@ -0,0 +1,70 @@ +package no.ntnu.idatx1005.demo.dbexample; + +import java.sql.*; + +public class SimpleJdbcExample { + + 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 static void main(String[] args) { + + try { + // get database connection + Connection connection = DriverManager.getConnection(DATABASE_URL, username, password); + addUser(connection); + getUsers(connection); + + //modifyUser(connection); + //deleteUser(connection); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + private static void getUsers(Connection connection) throws SQLException { + // define a statement with SQL query (get data) + PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM userinfo"); + + // execute statement and get resultset + ResultSet resultSet = preparedStatement.executeQuery(); + + //iterate over resultset + while (resultSet.next()) { // moves its cursor in a forward direction + System.out.println("userId:"+resultSet.getInt(1)+ + " Username:"+resultSet.getString(2)); + } + } + + private static void deleteUser(Connection connection) throws SQLException { + // define a statement with SQL query (to add a new record) + String sql = "DELETE FROM userinfo WHERE \"userId\" = ?"; + PreparedStatement preparedStatement = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); + preparedStatement.setInt(1, 100); // userId to update + int rowsDeleted = preparedStatement.executeUpdate(); + System.out.println("Rows deleted: " + rowsDeleted); + } + + private static void modifyUser(Connection connection) throws SQLException { + // define a statement with SQL query (to add a new record) + String sql = "UPDATE userinfo SET \"username\" = ? WHERE \"userId\" = ?"; + PreparedStatement preparedStatement = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); + preparedStatement.setString(1, "John Doe"); // new username + preparedStatement.setInt(2, 100); // userId to update + int rowsUpdated = preparedStatement.executeUpdate(); + System.out.println("Rows updated: " + rowsUpdated); + } + + private static void addUser(Connection connection) throws SQLException { + // define a statement with SQL query (to add a new record) + String sql = "INSERT INTO userinfo (\"userId\", \"username\") VALUES (?, ?)"; + PreparedStatement preparedStatement = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); + preparedStatement.setObject(1, 100, Types.OTHER); + preparedStatement.setString(2, "John"); + preparedStatement.executeUpdate(); + } + + +}