From a299226547b15de587dad614f07ce459d01f3601 Mon Sep 17 00:00:00 2001 From: Kamal Wickramanayake Date: Sun, 3 May 2026 14:32:16 +0530 Subject: Added jdbc demo sample application --- database/jdbc/demo/.gitignore | 1 + database/jdbc/demo/.mvn/jvm.config | 0 database/jdbc/demo/.mvn/maven.config | 0 database/jdbc/demo/.vscode/settings.json | 3 + database/jdbc/demo/README | 18 ++++ database/jdbc/demo/bank.sql | 9 ++ database/jdbc/demo/pom.xml | 103 +++++++++++++++++++++ .../demo/src/main/java/com/example/jdbc/App.java | 40 ++++++++ .../src/test/java/com/example/jdbc/AppTest.java | 19 ++++ 9 files changed, 193 insertions(+) create mode 100644 database/jdbc/demo/.gitignore create mode 100644 database/jdbc/demo/.mvn/jvm.config create mode 100644 database/jdbc/demo/.mvn/maven.config create mode 100644 database/jdbc/demo/.vscode/settings.json create mode 100644 database/jdbc/demo/README create mode 100644 database/jdbc/demo/bank.sql create mode 100644 database/jdbc/demo/pom.xml create mode 100644 database/jdbc/demo/src/main/java/com/example/jdbc/App.java create mode 100644 database/jdbc/demo/src/test/java/com/example/jdbc/AppTest.java (limited to 'database/jdbc/demo') diff --git a/database/jdbc/demo/.gitignore b/database/jdbc/demo/.gitignore new file mode 100644 index 0000000..eb5a316 --- /dev/null +++ b/database/jdbc/demo/.gitignore @@ -0,0 +1 @@ +target diff --git a/database/jdbc/demo/.mvn/jvm.config b/database/jdbc/demo/.mvn/jvm.config new file mode 100644 index 0000000..e69de29 diff --git a/database/jdbc/demo/.mvn/maven.config b/database/jdbc/demo/.mvn/maven.config new file mode 100644 index 0000000..e69de29 diff --git a/database/jdbc/demo/.vscode/settings.json b/database/jdbc/demo/.vscode/settings.json new file mode 100644 index 0000000..c5f3f6b --- /dev/null +++ b/database/jdbc/demo/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "java.configuration.updateBuildConfiguration": "interactive" +} \ No newline at end of file diff --git a/database/jdbc/demo/README b/database/jdbc/demo/README new file mode 100644 index 0000000..34d2481 --- /dev/null +++ b/database/jdbc/demo/README @@ -0,0 +1,18 @@ +Create a Maven project. + - Update Maven archetype catalog + - Specify quickstart archetype + - Update Java version to 25 + +Add MariaDB JDBC driver to pom.xml + +Search Google and get JDBC sample code. + +Update your App class code with Google returned sample code. Adjust the import statements. + +Specify your MariaDB database connection details in the class. + +Adjust the SQL quiery. + +Adjust the results printing code to match with your database table columns. + +Run the App class. \ No newline at end of file diff --git a/database/jdbc/demo/bank.sql b/database/jdbc/demo/bank.sql new file mode 100644 index 0000000..42b0382 --- /dev/null +++ b/database/jdbc/demo/bank.sql @@ -0,0 +1,9 @@ +create table user ( + id int auto_increment primary key, + name text, + email text +); + +insert into user (name, email) values ('Kamal', 'kamal@inbox.lk'); +insert into user (name, email) values ('Kamal2', 'kamal2@inbox.lk'); +insert into user (name, email) values ('Kamal3', 'kamal3@inbox.lk'); \ No newline at end of file diff --git a/database/jdbc/demo/pom.xml b/database/jdbc/demo/pom.xml new file mode 100644 index 0000000..4e16387 --- /dev/null +++ b/database/jdbc/demo/pom.xml @@ -0,0 +1,103 @@ + + + 4.0.0 + + com.example.jdbc + demo + 1.0-SNAPSHOT + + demo + + http://www.example.com + + + UTF-8 + 25 + + + + + + org.junit + junit-bom + 5.11.0 + pom + import + + + + + + + + org.mariadb.jdbc + mariadb-java-client + 3.5.8 + compile + + + + org.junit.jupiter + junit-jupiter-api + test + + + + org.junit.jupiter + junit-jupiter-params + test + + + + + + + + + maven-clean-plugin + 3.4.0 + + + + maven-resources-plugin + 3.3.1 + + + maven-compiler-plugin + 3.13.0 + + + maven-surefire-plugin + 3.3.0 + + + maven-jar-plugin + 3.4.2 + + + maven-install-plugin + 3.1.2 + + + maven-deploy-plugin + 3.1.2 + + + + maven-site-plugin + 3.12.1 + + + maven-project-info-reports-plugin + 3.6.1 + + + + + \ No newline at end of file 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); + } +} -- cgit v1.2.3