diff options
Diffstat (limited to 'database/jdbc/demo/src')
| -rw-r--r-- | database/jdbc/demo/src/main/java/com/example/jdbc/App.java | 40 | ||||
| -rw-r--r-- | database/jdbc/demo/src/test/java/com/example/jdbc/AppTest.java | 19 |
2 files changed, 59 insertions, 0 deletions
diff --git a/database/jdbc/demo/src/main/java/com/example/jdbc/App.java b/database/jdbc/demo/src/main/java/com/example/jdbc/App.java new file mode 100644 index 0000000..d3abc90 --- /dev/null +++ b/database/jdbc/demo/src/main/java/com/example/jdbc/App.java @@ -0,0 +1,40 @@ +package com.example.jdbc; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.ResultSet; +import java.sql.Statement; + +/** + * Hello world! + */ +public class App { + + static final String URL = "jdbc:mariadb://localhost:3306/bank"; + static final String USER = "bankuser"; + static final String PASS = "bankpwd"; + static final String QUERY = "SELECT id, name, email FROM user"; + + public static void main(String[] args) { + // Step 1: Open a connection, create a statement, and execute the query + try (Connection conn = DriverManager.getConnection(URL, USER, PASS); + Statement stmt = conn.createStatement(); + ResultSet rs = stmt.executeQuery(QUERY)) { + + System.out.println("Connected to the database successfully!"); + + // Step 2: Iterate through the result set and display the data + while (rs.next()) { + // Retrieve data by column name or index (starting at 1) + int id = rs.getInt("id"); + String name = rs.getString("name"); + String email = rs.getString("email"); + + // Print the results + System.out.println("ID: " + id + ", Name: " + name + ", Email: " + email); + } + } catch (Exception e) { + e.printStackTrace(); // Handle any SQL or connection errors + } + } +} diff --git a/database/jdbc/demo/src/test/java/com/example/jdbc/AppTest.java b/database/jdbc/demo/src/test/java/com/example/jdbc/AppTest.java new file mode 100644 index 0000000..ccadfcc --- /dev/null +++ b/database/jdbc/demo/src/test/java/com/example/jdbc/AppTest.java @@ -0,0 +1,19 @@ +package com.example.jdbc; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +import org.junit.jupiter.api.Test; + +/** + * Unit test for simple App. + */ +public class AppTest { + + /** + * Rigorous Test :-) + */ + @Test + public void shouldAnswerWithTrue() { + assertTrue(true); + } +} |
