-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Docker compose and added SimpleJdbcExample.java
- Loading branch information
suryabahadurkathayat
committed
Mar 3, 2026
1 parent
03e933e
commit fa6be6a
Showing
2 changed files
with
73 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
src/main/java/no/ntnu/idatx1005/demo/dbexample/SimpleJdbcExample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | ||
| } | ||
|
|
||
|
|
||
| } |