Java Code Examples for org.h2.tools.RunScript#execute()

The following examples show how to use org.h2.tools.RunScript#execute() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestServer.java    From light-oauth2 with Apache License 2.0 6 votes vote down vote up
private TestServer() {
    DataSource ds = (DataSource) SingletonServiceFactory.getBean(DataSource.class);
    try (Connection connection = ds.getConnection()) {
        String schemaResourceName = "/create_h2.sql";
        InputStream in = TestServer.class.getResourceAsStream(schemaResourceName);

        if (in == null) {
            throw new RuntimeException("Failed to load resource: " + schemaResourceName);
        }
        InputStreamReader reader = new InputStreamReader(in, UTF_8);
        RunScript.execute(connection, reader);

    } catch (SQLException e) {
        e.printStackTrace();
    }

}
 
Example 2
Source File: AuditHandlerTest.java    From light-oauth2 with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void runOnceBeforeClass() {
    System.out.println("@BeforeClass - runOnceBeforeClass");
    DataSource ds = (DataSource) SingletonServiceFactory.getBean(DataSource.class);
    try (Connection connection = ds.getConnection()) {
        String schemaResourceName = "/create_h2.sql";
        InputStream in = AuditHandlerTest.class.getResourceAsStream(schemaResourceName);

        if (in == null) {
            throw new RuntimeException("Failed to load resource: " + schemaResourceName);
        }
        InputStreamReader reader = new InputStreamReader(in, UTF_8);
        RunScript.execute(connection, reader);

    } catch (SQLException e) {
        e.printStackTrace();
    }

}
 
Example 3
Source File: TestServer.java    From light-oauth2 with Apache License 2.0 6 votes vote down vote up
private TestServer() {
    DataSource ds = (DataSource) SingletonServiceFactory.getBean(DataSource.class);
    try (Connection connection = ds.getConnection()) {
        String schemaResourceName = "/create_h2.sql";
        InputStream in = TestServer.class.getResourceAsStream(schemaResourceName);

        if (in == null) {
            throw new RuntimeException("Failed to load resource: " + schemaResourceName);
        }
        InputStreamReader reader = new InputStreamReader(in, UTF_8);
        RunScript.execute(connection, reader);

    } catch (SQLException e) {
        e.printStackTrace();
    }

}
 
Example 4
Source File: TestServer.java    From light-oauth2 with Apache License 2.0 6 votes vote down vote up
private TestServer() {
    DataSource ds = (DataSource) SingletonServiceFactory.getBean(DataSource.class);
    try (Connection connection = ds.getConnection()) {
        String schemaResourceName = "/create_h2.sql";
        InputStream in = TestServer.class.getResourceAsStream(schemaResourceName);

        if (in == null) {
            throw new RuntimeException("Failed to load resource: " + schemaResourceName);
        }
        InputStreamReader reader = new InputStreamReader(in, UTF_8);
        RunScript.execute(connection, reader);

    } catch (SQLException e) {
        e.printStackTrace();
    }

}
 
Example 5
Source File: TestServer.java    From light-oauth2 with Apache License 2.0 6 votes vote down vote up
private TestServer() {
    DataSource ds = (DataSource) SingletonServiceFactory.getBean(DataSource.class);
    try (Connection connection = ds.getConnection()) {
        String schemaResourceName = "/create_h2.sql";
        InputStream in = TestServer.class.getResourceAsStream(schemaResourceName);

        if (in == null) {
            throw new RuntimeException("Failed to load resource: " + schemaResourceName);
        }
        InputStreamReader reader = new InputStreamReader(in, UTF_8);
        RunScript.execute(connection, reader);

    } catch (SQLException e) {
        e.printStackTrace();
    }

}
 
Example 6
Source File: QueryDataSourceTest.java    From obevo with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void loadDriverTest() throws Exception {
    Class.forName("org.h2.Driver");
    activeConn = getConnection();

    Connection conn = getConnection();
    RunScript.execute(conn, new FileReader("src/test/resources/testdb.ddl"));
    conn.close();
}
 
Example 7
Source File: MultiDatabaseTestCase.java    From Zebra with Apache License 2.0 5 votes vote down vote up
@Before
public void createTableAndImportDataSet() throws Exception {
	for (DataSourceEntry entry : getDataSourceEntryArray()) {
		RunScript.execute(entry.getJdbcUrl(), USER, PASSWORD, getSchema(), "UTF8", false);
		cleanlyInsert(JDBC_DRIVER, entry.getJdbcUrl(), USER, PASSWORD, readDataSet(entry.getDataSets()));
		if (entry.isWrite()) {
			writeDs = createRealDataSource(entry.getJdbcUrl(), USER, PASSWORD, JDBC_DRIVER);
		} else {
			readDses.add(createRealDataSource(entry.getJdbcUrl(), USER, PASSWORD, JDBC_DRIVER));
		}
	}
}
 
Example 8
Source File: H2DBTestServer.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void shutdown() {
  try (Connection conn = getDataSource().getConnection()) {
    RunScript.execute(conn, new StringReader("SHUTDOWN"));
  } catch (SQLException x) {
    throw new RuntimeException(x);
  }
}
 
Example 9
Source File: SchemaEnvironmentManager.java    From shardingsphere with Apache License 2.0 5 votes vote down vote up
private static void dropTable(final SchemaEnvironment databaseEnvironmentSchema, final DatabaseType databaseType) {
    for (String each : databaseEnvironmentSchema.getDatabases()) {
        DataSource dataSource = DataSourceUtil.createDataSource(databaseType, each);
        try (Connection connection = dataSource.getConnection();
             StringReader stringReader = new StringReader(Joiner.on(";\n").join(databaseEnvironmentSchema.getTableDropSQLs()))) {
            RunScript.execute(connection, stringReader);
        } catch (final SQLException ex) {
            // TODO table maybe not exist
        }
    }
}
 
Example 10
Source File: AbstractShardingSphereDataSourceForShardingTest.java    From shardingsphere with Apache License 2.0 5 votes vote down vote up
@Before
public void initTable() {
    try {
        ShardingSphereConnection conn = shardingSphereDataSource.getConnection();
        RunScript.execute(conn, new InputStreamReader(AbstractSQLTest.class.getClassLoader().getResourceAsStream("jdbc_data.sql")));
        conn.close();
    } catch (final SQLException ex) {
        ex.printStackTrace();
    }
}
 
Example 11
Source File: TransactionTest.java    From registry with Apache License 2.0 5 votes vote down vote up
private void runScript(String fileName) throws SQLException, IOException {
    Connection connection = null;
    try {
        connection = connectionBuilder.getConnection();
        RunScript.execute(connection, load(fileName));
    } finally {
        connection.close();
    }
}
 
Example 12
Source File: CamelSqlTest.java    From jbpm-work-items with Apache License 2.0 5 votes vote down vote up
/**
 * Prepares test table containing a single data row.
 */
private static void setupDb() throws SQLException, URISyntaxException {
    File script = new File(CamelSqlTest.class.getResource("/init-db.sql").toURI());
    RunScript.execute("jdbc:h2:mem:jbpm-db;MVCC=true",
                      "sa",
                      "",
                      script.getAbsolutePath(),
                      StandardCharsets.UTF_8,
                      false);
}
 
Example 13
Source File: AbstractShardingSphereDataSourceForEncryptTest.java    From shardingsphere with Apache License 2.0 5 votes vote down vote up
@Before
public void initTable() {
    try (ShardingSphereConnection connection = encryptDataSource.getConnection()) {
        RunScript.execute(connection, new InputStreamReader(AbstractSQLTest.class.getClassLoader().getResourceAsStream("encrypt_data.sql")));
    } catch (final SQLException ex) {
        ex.printStackTrace();
    }
}
 
Example 14
Source File: DbH2ServerStartup.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * Populate sample database.
 *
 * @throws SQLException if
 */
public static void populateDatabase() throws SQLException {
    // Try to connect to database TCP server.
    JdbcConnectionPool dataSrc = JdbcConnectionPool.create("jdbc:h2:tcp://localhost/mem:ExampleDb", "sa", "");

    // Create Person table in database.
    RunScript.execute(dataSrc.getConnection(), new StringReader(CREATE_PERSON_TABLE));

    // Populates Person table with sample data in database.
    RunScript.execute(dataSrc.getConnection(), new StringReader(POPULATE_PERSON_TABLE));
}
 
Example 15
Source File: ActionSetToken.java    From o2oa with GNU Affero General Public License v3.0 5 votes vote down vote up
private void changeInternalDataServerPassword(String oldPassword, String newPassword) throws Exception {
	org.h2.Driver.load();
	for (Entry<String, DataServer> en : Config.nodes().dataServers().entrySet()) {
		DataServer o = en.getValue();
		if (BooleanUtils.isTrue(o.getEnable())) {
			try (Connection conn = DriverManager.getConnection(
					"jdbc:h2:tcp://" + en.getKey() + ":" + o.getTcpPort() + "/X", "sa", oldPassword)) {
				RunScript.execute(conn, new StringReader("ALTER USER SA SET PASSWORD '" + newPassword + "'"));
			} catch (Exception e) {
				throw new Exception("Verify that the dataServer:" + en.getKey()
						+ " is started and that the dataServer password is updated synchronously.", e);
			}
		}
	}
}
 
Example 16
Source File: DbH2ServerStartup.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * Start H2 database TCP server.
 *
 * @param args Command line arguments, none required.
 * @throws IgniteException If start H2 database TCP server failed.
 */
public static void main(String[] args) throws IgniteException {
    try {
        // Start H2 database TCP server in order to access sample in-memory database from other processes.
        Server.createTcpServer("-tcpDaemon").start();

        populateDatabase();

        // Try to connect to database TCP server.
        JdbcConnectionPool dataSrc = JdbcConnectionPool.create("jdbc:h2:tcp://localhost/mem:ExampleDb", "sa", "");

        // Create Person table in database.
        RunScript.execute(dataSrc.getConnection(), new StringReader(CREATE_PERSON_TABLE));

        // Populates Person table with sample data in database.
        RunScript.execute(dataSrc.getConnection(), new StringReader(POPULATE_PERSON_TABLE));
    }
    catch (SQLException e) {
        throw new IgniteException("Failed to start database TCP server", e);
    }

    try {
        do {
            System.out.println("Type 'q' and press 'Enter' to stop H2 TCP server...");
        }
        while ('q' != System.in.read());
    }
    catch (IOException ignored) {
        // No-op.
    }
}
 
Example 17
Source File: H2JpaCleaner.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void clean() {
  super.clean();
  if (server != null) {
    server.shutdown();
  } else {
    try (Connection conn = dataSource.getConnection()) {
      RunScript.execute(conn, new StringReader("SHUTDOWN"));
    } catch (SQLException x) {
      throw new RuntimeException(x.getMessage(), x);
    }
  }
}
 
Example 18
Source File: CleanupStrategyProviderTest.java    From jpa-unit with Apache License 2.0 4 votes vote down vote up
@BeforeClass
public static void createSchema() throws Exception {
    RunScript.execute(CONNECTION_URL, USER_NAME, PASSWORD, DB_SETUP_SCRIPT, StandardCharsets.UTF_8, false);
}
 
Example 19
Source File: H2DatabaseTestCase.java    From Zebra with Apache License 2.0 4 votes vote down vote up
@Before
public void createTableAndImportDataSet() throws Exception {
	RunScript.execute(JDBC_URL, USER, PASSWORD, getSchema(), "UTF8", false);
	cleanlyInsert(JDBC_DRIVER, JDBC_URL, USER, PASSWORD, readDataSet());
}
 
Example 20
Source File: FlywaySchemaInitializerTest.java    From che with Eclipse Public License 2.0 4 votes vote down vote up
private ResultSet execQuery(String query) throws SQLException {
  try (Connection conn = dataSource.getConnection()) {
    return RunScript.execute(conn, new StringReader(query));
  }
}