diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5ff6309 --- /dev/null +++ b/.gitignore @@ -0,0 +1,38 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### IntelliJ IDEA ### +.idea/modules.xml +.idea/jarRepositories.xml +.idea/compiler.xml +.idea/libraries/ +*.iws +*.iml +*.ipr + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..aa00ffa --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..039a9d1 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..8ad2ca3 --- /dev/null +++ b/pom.xml @@ -0,0 +1,24 @@ + + + 4.0.0 + + org.example + H-ch-T-Books + 1.0-SNAPSHOT + + + 22 + 22 + UTF-8 + + + + com.h2database + h2 + 2.1.214 + + + + \ No newline at end of file diff --git a/src/main/java/com/example/h2demo/H2DatabaseDemo.java b/src/main/java/com/example/h2demo/H2DatabaseDemo.java new file mode 100644 index 0000000..ffc4056 --- /dev/null +++ b/src/main/java/com/example/h2demo/H2DatabaseDemo.java @@ -0,0 +1,71 @@ +package com.example.h2demo; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.ResultSet; +import java.sql.Statement; + +public class H2DatabaseDemo { + public static void main(String[] args) { + Connection connection = null; + Statement statement = null; + + try { + // Load H2 driver + Class.forName("org.h2.Driver"); + + // Establish a connection + connection = DriverManager.getConnection("jdbc:h2:~/testdb", "sa", ""); + + // Start the H2 Web Console + org.h2.tools.Server.startWebServer(connection); + + // Create a statement + statement = connection.createStatement(); + + // Drop the table if it already exists + String dropTableSQL = "DROP TABLE IF EXISTS books"; + statement.execute(dropTableSQL); + + + // Create a table in the H2 database + String createTableSQL = "CREATE TABLE books (" + + "id INT PRIMARY KEY, " + + "title VARCHAR(255), " + + "author VARCHAR(255), " + + "publisherYear VARCHAR(4), " + + "genre VARCHAR(255), " + + "description TEXT)"; + statement.execute(createTableSQL); + + // Insert data into the table + String insertDataSQL = "INSERT INTO books (id, title, author, publisherYear, genre, description) VALUES " + + "(1, 'Becoming', 'Michelle Obama', '2018', 'Memoir, Non-fiction', 'Inspiring memoir of Michelle Obama''s life journey')," + + "(2, 'The Catcher in the Rye', 'J.D. Salinger', '1951', 'Fiction, Coming-of-Age', 'A story about teenage rebellion and alienation');"; + statement.execute(insertDataSQL); + + // Query the table and print out the data + String selectSQL = "SELECT * FROM books"; + ResultSet resultSet = statement.executeQuery(selectSQL); + + while (resultSet.next()) { + System.out.println("ID: " + resultSet.getInt("id")); + System.out.println("Title: " + resultSet.getString("title")); + System.out.println("Author: " + resultSet.getString("author")); + System.out.println("Year: " + resultSet.getString("publisherYear")); + System.out.println("Genre: " + resultSet.getString("genre")); + System.out.println("Description: " + resultSet.getString("description")); + System.out.println("---------------------------"); + } + + } catch (Exception e) { + e.printStackTrace(); + } finally { + try { + if (statement != null) statement.close(); + if (connection != null) connection.close(); + } catch (Exception e) { + e.printStackTrace(); + } + } + } +} diff --git a/src/main/java/com/example/h2demo/Main.java b/src/main/java/com/example/h2demo/Main.java new file mode 100644 index 0000000..5ce1649 --- /dev/null +++ b/src/main/java/com/example/h2demo/Main.java @@ -0,0 +1,7 @@ +package com.example.h2demo; + +public class Main { + public static void main(String[] args) { + System.out.println("Hello world!"); + } +} \ No newline at end of file