Skip to content

Commit

Permalink
Docker compose and added SimpleJdbcExample.java
Browse files Browse the repository at this point in the history
  • Loading branch information
suryabahadurkathayat committed Mar 3, 2026
1 parent 03e933e commit fa6be6a
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 4 deletions.
7 changes: 3 additions & 4 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ version: "3.7"

services:
postgres:
image: postgres:15.1
image: postgres:latest
restart: always
environment:
POSTGRES_DB: idatt1005-db
Expand All @@ -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:
Original file line number Diff line number Diff line change
@@ -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();
}


}

0 comments on commit fa6be6a

Please sign in to comment.