diff options
| author | Kamal Wickramanayake <kamal@inbox.lk> | 2026-05-03 14:32:16 +0530 |
|---|---|---|
| committer | Kamal Wickramanayake <kamal@inbox.lk> | 2026-05-03 14:32:16 +0530 |
| commit | a299226547b15de587dad614f07ce459d01f3601 (patch) | |
| tree | a917b98c412123ad4c5362bbb6d752dc8007e047 /database | |
| parent | cdae21a9987ae947f58662ac1e7c2d02a469629a (diff) | |
Added jdbc demo sample application
Diffstat (limited to 'database')
| -rw-r--r-- | database/jdbc/demo/.gitignore | 1 | ||||
| -rw-r--r-- | database/jdbc/demo/.mvn/jvm.config | 0 | ||||
| -rw-r--r-- | database/jdbc/demo/.mvn/maven.config | 0 | ||||
| -rw-r--r-- | database/jdbc/demo/.vscode/settings.json | 3 | ||||
| -rw-r--r-- | database/jdbc/demo/README | 18 | ||||
| -rw-r--r-- | database/jdbc/demo/bank.sql | 9 | ||||
| -rw-r--r-- | database/jdbc/demo/pom.xml | 103 | ||||
| -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 |
9 files changed, 193 insertions, 0 deletions
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 --- /dev/null +++ b/database/jdbc/demo/.mvn/jvm.config diff --git a/database/jdbc/demo/.mvn/maven.config b/database/jdbc/demo/.mvn/maven.config new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/database/jdbc/demo/.mvn/maven.config 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 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <groupId>com.example.jdbc</groupId> + <artifactId>demo</artifactId> + <version>1.0-SNAPSHOT</version> + + <name>demo</name> + <!-- FIXME change it to the project's website --> + <url>http://www.example.com</url> + + <properties> + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> + <maven.compiler.release>25</maven.compiler.release> + </properties> + + <dependencyManagement> + <dependencies> + <dependency> + <groupId>org.junit</groupId> + <artifactId>junit-bom</artifactId> + <version>5.11.0</version> + <type>pom</type> + <scope>import</scope> + </dependency> + </dependencies> + </dependencyManagement> + + <dependencies> + <!-- Source: https://mvnrepository.com/artifact/org.mariadb.jdbc/mariadb-java-client --> + <dependency> + <groupId>org.mariadb.jdbc</groupId> + <artifactId>mariadb-java-client</artifactId> + <version>3.5.8</version> + <scope>compile</scope> + </dependency> + + <dependency> + <groupId>org.junit.jupiter</groupId> + <artifactId>junit-jupiter-api</artifactId> + <scope>test</scope> + </dependency> + <!-- Optionally: parameterized tests support --> + <dependency> + <groupId>org.junit.jupiter</groupId> + <artifactId>junit-jupiter-params</artifactId> + <scope>test</scope> + </dependency> + </dependencies> + + <build> + <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to + parent pom) --> + <plugins> + <!-- clean lifecycle, see + https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle --> + <plugin> + <artifactId>maven-clean-plugin</artifactId> + <version>3.4.0</version> + </plugin> + <!-- default lifecycle, jar packaging: see + https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging --> + <plugin> + <artifactId>maven-resources-plugin</artifactId> + <version>3.3.1</version> + </plugin> + <plugin> + <artifactId>maven-compiler-plugin</artifactId> + <version>3.13.0</version> + </plugin> + <plugin> + <artifactId>maven-surefire-plugin</artifactId> + <version>3.3.0</version> + </plugin> + <plugin> + <artifactId>maven-jar-plugin</artifactId> + <version>3.4.2</version> + </plugin> + <plugin> + <artifactId>maven-install-plugin</artifactId> + <version>3.1.2</version> + </plugin> + <plugin> + <artifactId>maven-deploy-plugin</artifactId> + <version>3.1.2</version> + </plugin> + <!-- site lifecycle, see + https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle --> + <plugin> + <artifactId>maven-site-plugin</artifactId> + <version>3.12.1</version> + </plugin> + <plugin> + <artifactId>maven-project-info-reports-plugin</artifactId> + <version>3.6.1</version> + </plugin> + </plugins> + </pluginManagement> + </build> +</project>
\ 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); + } +} |
