-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 27581b7
Showing
8 changed files
with
175 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!"); | ||
} | ||
} |