· 9 years ago · Jul 22, 2017, 09:08 PM
1import java.sql.Connection;
2import java.sql.DriverManager;
3import java.sql.PreparedStatement;
4import java.sql.SQLException;
5
6public class H2 {
7 private static final String FILLER = "ABCDEFGHIJKLMNOPRSTUWYZabcdefghijklmnoprstuwyz1234567890";
8 private static final int NUMBER_OF_INSERTIONS = 100000;
9
10 /**
11 * @param args
12 * @throws SQLException
13 */
14 public static void main(final String[] args) throws SQLException {
15 // final Connection connection =
16 // DriverManager.getConnection("jdbc:h2:db/TEST;DB_CLOSE_ON_EXIT=TRUE",
17 // "sa", null);
18 final Connection connection = DriverManager.getConnection(
19 "jdbc:h2:tcp://localhost/~/test", "sa", null);
20 connection.createStatement().executeUpdate("DROP TABLE IF EXISTS data");
21 connection
22 .createStatement()
23 .executeUpdate(
24 "CREATE TABLE `data` (`id` IDENTITY NOT NULL, `timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, `name` varchar(128) DEFAULT NULL,`author` varchar(128) DEFAULT NULL, `notes` varchar(128) DEFAULT NULL);");
25 final PreparedStatement preparedStatement = connection
26 .prepareStatement("INSERT INTO data (name, author, notes) VALUES(?,?,?)");
27
28 int numberOfInsertions = NUMBER_OF_INSERTIONS;
29 final long startTime = System.currentTimeMillis();
30
31 while (numberOfInsertions-- > 0) {
32 preparedStatement.setString(1, FILLER);
33 preparedStatement.setString(2, FILLER);
34 preparedStatement.setString(3, FILLER);
35 preparedStatement.executeUpdate();
36 }
37
38 final long stopTime = System.currentTimeMillis();
39 final long deltaSeconds = (stopTime - startTime) / 1000L;
40
41 preparedStatement.close();
42 connection.close();
43
44 System.out.println(NUMBER_OF_INSERTIONS / deltaSeconds + "/s");
45 }
46}