Skip to content

Commit

Permalink
the project has initialised
Browse files Browse the repository at this point in the history
  • Loading branch information
H-CHAOUAT committed Sep 10, 2024
0 parents commit 27581b7
Show file tree
Hide file tree
Showing 8 changed files with 175 additions and 0 deletions.
38 changes: 38 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?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>org.example</groupId>
<artifactId>H-ch-T-Books</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>22</maven.compiler.source>
<maven.compiler.target>22</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>2.1.214</version> <!-- Adjust the version if necessary -->
</dependency>
</dependencies>

</project>
71 changes: 71 additions & 0 deletions src/main/java/com/example/h2demo/H2DatabaseDemo.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
}
7 changes: 7 additions & 0 deletions src/main/java/com/example/h2demo/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.example.h2demo;

public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
}
}

0 comments on commit 27581b7

Please sign in to comment.