Debian Codegraph @

src/test/org/h2/samples/HelloWorld.java

raw
1 /*
2 * Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0,
3 * and the EPL 1.0 (https://h2database.com/html/license.html).
4 * Initial Developer: H2 Group
5 */
6 package org.h2.samples;
7
8 import java.sql.Connection;
9 import java.sql.DriverManager;
10 import java.sql.ResultSet;
11 import java.sql.Statement;
12 import org.h2.tools.DeleteDbFiles;
13
14 /**
15 * A very simple class that shows how to load the driver, create a database,
16 * create a table, and insert some data.
17 */
18 public class HelloWorld {
19
20 /**
21 * Called when ran from command line.
22 *
23 * @param args ignored
24 * @throws Exception on failure
25 */
26 public static void main(String... args) throws Exception {
27 // delete the database named 'test' in the user home directory
28 DeleteDbFiles.execute("~", "test", true);
29
30 Class.forName("org.h2.Driver");
31 Connection conn = DriverManager.getConnection("jdbc:h2:~/test");
32 Statement stat = conn.createStatement();
33
34 // this line would initialize the database
35 // from the SQL script file 'init.sql'
36 // stat.execute("runscript from 'init.sql'");
37
38 stat.execute("create table test(id int primary key, name varchar(255))");
39 stat.execute("insert into test values(1, 'Hello')");
40 ResultSet rs;
41 rs = stat.executeQuery("select * from test");
42 while (rs.next()) {
43 System.out.println(rs.getString("name"));
44 }
45 stat.close();
46 conn.close();
47 }
48
49 }