How to use ScriptStatementFailedException method of org.testcontainers.ext.ScriptUtils class

Best Testcontainers-java code snippet using org.testcontainers.ext.ScriptUtils.ScriptStatementFailedException

Source:ScyllaDatabaseDelegate.java Github

copy

Full Screen

...7import org.testcontainers.containers.ContainerState;8import org.testcontainers.scylla.ScyllaContainer;9import org.testcontainers.delegate.AbstractDatabaseDelegate;10import org.testcontainers.exception.ConnectionCreationException;11import org.testcontainers.ext.ScriptUtils.ScriptStatementFailedException;12/**13 * Scylla database delegate14 *15 * @author Eugeny Karpov16 * @author Jochen Schalanda17 */18public class ScyllaDatabaseDelegate extends AbstractDatabaseDelegate<Session> {19 private static final Logger log = LoggerFactory.getLogger(ScyllaDatabaseDelegate.class);20 private final ContainerState container;21 public ScyllaDatabaseDelegate(ContainerState container) {22 this.container = container;23 }24 @Override25 protected Session createNewConnection() {26 try {27 return ScyllaContainer.getCluster(container)28 .newSession();29 } catch (DriverException e) {30 log.error("Could not obtain Scylla connection");31 throw new ConnectionCreationException("Could not obtain Scylla connection", e);32 }33 }34 @Override35 public void execute(String statement, String scriptPath, int lineNumber, boolean continueOnError, boolean ignoreFailedDrops) {36 try {37 ResultSet result = getConnection().execute(statement);38 if (result.wasApplied()) {39 log.debug("Statement {} was applied", statement);40 } else {41 throw new ScriptStatementFailedException(statement, lineNumber, scriptPath);42 }43 } catch (DriverException e) {44 throw new ScriptStatementFailedException(statement, lineNumber, scriptPath, e);45 }46 }47 @Override48 protected void closeConnectionQuietly(Session session) {49 try {50 session.getCluster().close();51 } catch (Exception e) {52 log.error("Could not close Scylla connection", e);53 }54 }55}...

Full Screen

Full Screen

Source:CassandraDatabaseDelegate.java Github

copy

Full Screen

...7import org.testcontainers.containers.CassandraContainer;8import org.testcontainers.containers.ContainerState;9import org.testcontainers.delegate.AbstractDatabaseDelegate;10import org.testcontainers.exception.ConnectionCreationException;11import org.testcontainers.ext.ScriptUtils.ScriptStatementFailedException;12/**13 * Cassandra database delegate14 *15 * @author Eugeny Karpov16 */17@Slf4j18@RequiredArgsConstructor19public class CassandraDatabaseDelegate extends AbstractDatabaseDelegate<Session> {20 private final ContainerState container;21 @Override22 protected Session createNewConnection() {23 try {24 return CassandraContainer.getCluster(container)25 .newSession();26 } catch (DriverException e) {27 log.error("Could not obtain cassandra connection");28 throw new ConnectionCreationException("Could not obtain cassandra connection", e);29 }30 }31 @Override32 public void execute(String statement, String scriptPath, int lineNumber, boolean continueOnError, boolean ignoreFailedDrops) {33 try {34 ResultSet result = getConnection().execute(statement);35 if (result.wasApplied()) {36 log.debug("Statement {} was applied", statement);37 } else {38 throw new ScriptStatementFailedException(statement, lineNumber, scriptPath);39 }40 } catch (DriverException e) {41 throw new ScriptStatementFailedException(statement, lineNumber, scriptPath, e);42 }43 }44 @Override45 protected void closeConnectionQuietly(Session session) {46 try {47 session.getCluster().close();48 } catch (Exception e) {49 log.error("Could not close cassandra connection", e);50 }51 }52}...

Full Screen

Full Screen

ScriptStatementFailedException

Using AI Code Generation

copy

Full Screen

1import org.junit.Test;2import org.junit.runner.RunWith;3import org.springframework.beans.factory.annotation.Autowired;4import org.springframework.boot.test.context.SpringBootTest;5import org.springframework.core.io.ClassPathResource;6import org.springframework.jdbc.core.JdbcTemplate;7import org.springframework.test.context.junit4.SpringRunner;8import org.testcontainers.containers.PostgreSQLContainer;9import org.testcontainers.ext.ScriptUtils;10import javax.sql.DataSource;11import static org.assertj.core.api.Assertions.assertThat;12import static org.assertj.core.api.Assertions.assertThatThrownBy;13@RunWith(SpringRunner.class)14public class PostgresTest {15 private DataSource dataSource;16 public void testPostgres() {17 JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);18 assertThat(jdbcTemplate.queryForObject("SELECT 1", Integer.class)).isEqualTo(1);19 assertThat(jdbcTemplate.queryForObject("SELECT 2", Integer.class)).isEqualTo(2);20 }21 public void testPostgresScript() {22 JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);23 PostgreSQLContainer<?> postgres = (PostgreSQLContainer<?>) jdbcTemplate.getDataSource();24 postgres.withInitScript("test.sql");25 ScriptUtils.executeScript(postgres, new ClassPathResource("test.sql"));26 assertThat(jdbcTemplate.queryForObject("SELECT 1", Integer.class)).isEqualTo(1);27 assertThat(jdbcTemplate.queryForObject("SELECT 2", Integer.class)).isEqualTo(2);28 assertThatThrownBy(() -> ScriptUtils.executeScript(postgres, new ClassPathResource("test.sql")))29 .isInstanceOf(ScriptUtils.ScriptStatementFailedException.class);30 }31}32import org.junit.Test;33import org.junit.runner.RunWith;34import org.springframework.beans.factory.annotation.Autowired;35import org.springframework.boot.test.context.SpringBootTest;36import org.springframework.core.io.ClassPathResource;37import org.springframework.jdbc.core.JdbcTemplate;38import org.springframework.test.context.junit4.SpringRunner;39import org.testcontainers.containers.PostgreSQLContainer;40import org.testcontainers.ext.ScriptUtils;41import javax.sql.DataSource;42import static org.assertj.core.api.Assertions.assertThat;43import static org.assertj.core.api.Assertions.assertThatThrownBy;44@RunWith(SpringRunner.class)45public class PostgresTest {46 private DataSource dataSource;47 public void testPostgres() {48 JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);49 assertThat(jdbcTemplate.queryForObject("SELECT 1", Integer.class)).isEqualTo(1);

Full Screen

Full Screen

ScriptStatementFailedException

Using AI Code Generation

copy

Full Screen

1import org.testcontainers.containers.JdbcDatabaseContainer;2import org.testcontainers.containers.MySQLContainer;3import org.testcontainers.containers.output.Slf4jLogConsumer;4import org.testcontainers.ext.ScriptUtils;5import org.testcontainers.utility.MountableFile;6import java.io.IOException;7import java.sql.Connection;8import java.sql.DriverManager;9import java.sql.SQLException;10public class ScriptUtilsTest {11 public static void main(String[] args) throws SQLException, IOException, ScriptUtils.ScriptStatementFailedException {12 JdbcDatabaseContainer mysql = new MySQLContainer()13 .withLogConsumer(new Slf4jLogConsumer());14 mysql.start();15 Connection connection = DriverManager.getConnection(mysql.getJdbcUrl(), mysql.getUsername(), mysql.getPassword());16 String sql = "CREATE TABLE IF NOT EXISTS `test` (\n" +17 " `id` int(11) NOT NULL AUTO_INCREMENT,\n" +18 " `name` varchar(255) NOT NULL,\n" +19 " `age` int(11) NOT NULL,\n" +20 " PRIMARY KEY (`id`)\n" +21 ") ENGINE=InnoDB DEFAULT CHARSET=utf8;";22 ScriptUtils.runInitScript(connection, MountableFile.forString(sql));23 }24}25import org.testcontainers.containers.JdbcDatabaseContainer;26import org.testcontainers.containers.MySQLContainer;27import org.testcontainers.containers.output.Slf4jLogConsumer;28import org.testcontainers.ext.ScriptUtils;29import org.testcontainers.utility.MountableFile;30import java.io.IOException;31import java.sql.Connection;32import java.sql.DriverManager;33import java.sql.SQLException;34public class ScriptUtilsTest {35 public static void main(String[] args) throws SQLException, IOException, ScriptUtils.ScriptStatementFailedException {36 JdbcDatabaseContainer mysql = new MySQLContainer()37 .withLogConsumer(new Slf4jLogConsumer());38 mysql.start();39 Connection connection = DriverManager.getConnection(mysql.getJdbcUrl(), mysql.getUsername(), mysql.getPassword());40 String sql = "CREATE TABLE IF NOT EXISTS `test` (\n" +41 " `id` int(11) NOT NULL AUTO_INCREMENT,\n" +42 " `name` varchar(255) NOT NULL,\n" +43 " `age` int(11) NOT NULL,\n" +44 " PRIMARY KEY (`id`)\n"

Full Screen

Full Screen

ScriptStatementFailedException

Using AI Code Generation

copy

Full Screen

1import org.testcontainers.containers.JdbcDatabaseContainer;2import org.testcontainers.containers.PostgreSQLContainer;3import org.testcontainers.ext.ScriptUtils;4import org.testcontainers.utility.MountableFile;5import java.io.File;6import java.sql.Connection;7import java.sql.SQLException;8import java.util.List;9public class Test {10 public static void main(String[] args) throws SQLException, ScriptUtils.ScriptStatementFailedException {11 JdbcDatabaseContainer container = new PostgreSQLContainer();12 container.start();13 Connection connection = container.createConnection("");14 List<String> statements = ScriptUtils.readScript(MountableFile.forClasspathResource("test.sql"));15 ScriptUtils.executeSqlScript(connection, statements);16 }17}18CREATE TABLE IF NOT EXISTS test (id int);19INSERT INTO test (id) VALUES (1);

Full Screen

Full Screen

ScriptStatementFailedException

Using AI Code Generation

copy

Full Screen

1import org.testcontainers.containers.PostgreSQLContainer;2import org.testcontainers.ext.ScriptUtils;3import java.io.IOException;4import java.sql.Connection;5import java.sql.SQLException;6public class ScriptUtilsExample {7 public static void main(String[] args) throws IOException, SQLException {8 PostgreSQLContainer postgreSQLContainer = new PostgreSQLContainer("postgres:9.6.8")9 .withDatabaseName("test")10 .withUsername("test")11 .withPassword("test");12 postgreSQLContainer.start();13 Connection connection = postgreSQLContainer.createConnection("");14 try {15 ScriptUtils.runInitScript(connection, "1.sql");16 } catch (ScriptUtils.ScriptStatementFailedException e) {17 System.out.println(e.getMessage());18 }19 }20}21create table test_table (id int);22insert into test_table (id) values (1);23insert into test_table (id) values (2);24insert into test_table (id) values (3);25insert into test_table (id) values (4);26insert into test_table (id) values (5);27insert into test_table (id) values (6);28insert into test_table (id) values (7);29insert into test_table (id) values (8);30insert into test_table (id) values (9);31insert into test_table (id) values (10);32insert into test_table (id) values (11);33insert into test_table (id) values (12);34insert into test_table (id) values (13);35insert into test_table (id) values (14);36insert into test_table (id) values (15);37insert into test_table (id) values (16);38insert into test_table (id) values (17);39insert into test_table (id) values (18);40insert into test_table (id) values (19);41insert into test_table (id) values (20);42insert into test_table (id) values (21);43insert into test_table (id) values (22);44insert into test_table (id) values (23);45insert into test_table (id) values (24);46insert into test_table (id) values (25);47insert into test_table (id) values (26);48insert into test_table (id) values (27);49insert into test_table (id) values (28);50insert into test_table (id) values (29);51insert into test_table (id) values (30);

Full Screen

Full Screen

ScriptStatementFailedException

Using AI Code Generation

copy

Full Screen

1package org.testcontainers.ext;2import java.io.IOException;3import java.nio.charset.StandardCharsets;4import java.nio.file.Files;5import java.nio.file.Path;6import java.nio.file.Paths;7import java.sql.SQLException;8import java.util.stream.Stream;9import org.testcontainers.jdbc.JdbcDatabaseContainer;10public class ScriptUtils {11 public static void execute(JdbcDatabaseContainer<?> container, String scriptPath) throws SQLException, ScriptStatementFailedException {12 Path script = Paths.get(scriptPath);13 try (Stream<String> lines = Files.lines(script, StandardCharsets.UTF_8)) {14 lines.forEach(line -> {15 try {16 container.execute(line);17 } catch (SQLException e) {18 throw new ScriptStatementFailedException("Failed to execute statement: " + line, e);19 }20 });21 } catch (IOException e) {22 throw new RuntimeException("Failed to read script file: " + scriptPath, e);23 }24 }25}26package org.testcontainers.ext;27public class ScriptStatementFailedException extends RuntimeException {28 public ScriptStatementFailedException(String message, Throwable cause) {29 super(message, cause);30 }31}32package org.testcontainers.ext;33public class ScriptUtilsTest {34 public static void main(String[] args) {35 }36}37package org.testcontainers.ext;38import java.sql.SQLException;39import org.testcontainers.containers.MySQLContainer;40public class ScriptUtilsTest {41 public static void main(String[] args) throws SQLException, ScriptStatementFailedException {42 try (MySQLContainer<?> container = new MySQLContainer<>()) {43 container.start();44 ScriptUtils.execute(container, "src/test/resources/init.sql");45 }46 }47}48CREATE TABLE IF NOT EXISTS test_table (id INT PRIMARY KEY);49INSERT INTO test_table VALUES (1);50INSERT INTO test_table VALUES (2);51INSERT INTO test_table VALUES (3);52INSERT INTO test_table VALUES (4);53INSERT INTO test_table VALUES (5);

Full Screen

Full Screen

ScriptStatementFailedException

Using AI Code Generation

copy

Full Screen

1import org.testcontainers.containers.JdbcDatabaseContainer;2import org.testcontainers.ext.ScriptUtils;3import org.testcontainers.jdbc.ContainerDatabaseDriver;4import org.testcontainers.jdbc.ContainerDatabaseDriverDelegate;5import java.sql.Connection;6import java.sql.SQLException;7import java.util.List;8import java.util.stream.Collectors;9public class ScriptUtilsTest {10 public static void main(String[] args) throws SQLException {11 JdbcDatabaseContainer container = new JdbcDatabaseContainer("mysql:5.7.22") {12 };13 container.start();14 String jdbcUrl = container.getJdbcUrl();15 String username = container.getUsername();16 String password = container.getPassword();17 Connection connection = ContainerDatabaseDriverDelegate.createConnection(jdbcUrl, username, password);18 String script = "CREATE TABLE IF NOT EXISTS `test` (\n" +19 " `id` int(11) NOT NULL AUTO_INCREMENT,\n" +20 " `name` varchar(255) DEFAULT NULL,\n" +21 " PRIMARY KEY (`id`)\n" +22 ");\n" +23 "INSERT INTO `test` (`name`) VALUES ('test1');\n" +24 "INSERT INTO `test` (`name`) VALUES ('test2');\n" +25 "INSERT INTO `test` (`name`) VALUES ('test3');\n" +26 "INSERT INTO `test` (`name`) VALUES ('test4');\n" +27 "INSERT INTO `test` (`name`) VALUES ('test5');\n" +28 "INSERT INTO `test` (`name`) VALUES ('test6');\n" +29 "INSERT INTO `test` (`name`) VALUES ('test7');\n" +30 "INSERT INTO `test` (`name`) VALUES ('test8');\n" +31 "INSERT INTO `test` (`name`) VALUES ('test9');\n" +32 "INSERT INTO `test` (`name`) VALUES ('test10');\n" +33 "INSERT INTO `test` (`name`) VALUES ('test11');\n" +34 "INSERT INTO `test` (`name`) VALUES ('test12');\n" +35 "INSERT INTO `test` (`name`) VALUES ('test13');\n" +36 "INSERT INTO `test` (`name`) VALUES ('test14');\n" +37 "INSERT INTO `test` (`name`) VALUES ('test15');

Full Screen

Full Screen

ScriptStatementFailedException

Using AI Code Generation

copy

Full Screen

1package org.testcontainers.ext;2import java.io.IOException;3import java.sql.Connection;4import java.sql.SQLException;5import java.util.ArrayList;6import java.util.List;7import org.testcontainers.jdbc.ContainerDatabaseDriver;8public class ScriptUtils {9 public static void executeSqlScript(Connection con, String script) throws ScriptStatementFailedException, SQLException, IOException {10 List<String> statementList = new ArrayList<>();11 statementList.add(script);12 List<String> failedStatements = new ArrayList<>();13 try (Connection connection = ContainerDatabaseDriver.createConnection(con)) {14 for (String statement : statementList) {15 try {16 connection.createStatement().execute(statement);17 } catch (SQLException e) {18 failedStatements.add(statement);19 }20 }21 }22 if (!failedStatements.isEmpty()) {23 throw new ScriptStatementFailedException(failedStatements);24 }25 }26}27import org.testcontainers.ext.ScriptStatementFailedException;28import org.testcontainers.ext.ScriptUtils;29import org.testcontainers.jdbc.ContainerDatabaseDriver;30import java.sql.Connection;31import java.sql.SQLException;32import java.util.ArrayList;33import java.util.List;34public class TestContainer {35 public static void main(String[] args) throws SQLException, IOException, ScriptStatementFailedException {36 String script = "create table user (id int, name varchar(20));";37 List<String> statementList = new ArrayList<>();38 statementList.add(script);39 List<String> failedStatements = new ArrayList<>();40 try (Connection connection = ContainerDatabaseDriver.createConnection(con)) {41 for (String statement : statementList) {42 try {43 connection.createStatement().execute(statement);44 } catch (SQLException e) {45 failedStatements.add(statement);46 }47 }48 }49 if (!failedStatements.isEmpty()) {50 throw new ScriptStatementFailedException(failedStatements);51 }52 }53}54create table user (id int, name varchar(20));55at org.testcontainers.ext.ScriptUtils.executeSqlScript(ScriptUtils.java:41)56at TestContainer.main(2.java:21)

Full Screen

Full Screen

ScriptStatementFailedException

Using AI Code Generation

copy

Full Screen

1public static void main(String[] args) throws Exception {2 try (Connection connection = DriverManager.getConnection("jdbc:h2:mem:db;DB_CLOSE_DELAY=-1", "sa", "")) {3 ScriptUtils.executeSqlScript(connection, new ClassPathResource("1.sql"));4 } catch (ScriptStatementFailedException e) {5 System.out.println(e.getStatement());6 }7}8CREATE TABLE TEST (ID INT PRIMARY KEY, NAME VARCHAR(255));9INSERT INTO TEST VALUES (1, 'TEST');10INSERT INTO TEST VALUES (1, 'TEST')

Full Screen

Full Screen

ScriptStatementFailedException

Using AI Code Generation

copy

Full Screen

1package org.testcontainers.ext;2import java.sql.Connection;3import java.sql.SQLException;4import java.sql.Statement;5import java.util.ArrayList;6import java.util.List;7import org.testcontainers.jdbc.ConnectionUrl;8import org.testcontainers.jdbc.ContainerDatabaseDriver;9public class ScriptUtils {10 public static void executeScript(Connection connection, String script) throws SQLException {11 List<String> statements = new ArrayList<>();12 StringBuilder currentStatement = new StringBuilder();13 String[] lines = script.split("\r14");15 for (String line : lines) {16 line = line.trim();17 if (line.startsWith("--")) {18 continue;19 }20 if (line.endsWith(";")) {21 currentStatement.append(line, 0, line.length() - 1);22 statements.add(currentStatement.toString());23 currentStatement = new StringBuilder();24 } else {25 currentStatement.append(line).append("26");27 }28 }29 for (String statement : statements) {30 try (Statement stmt = connection.createStatement()) {31 stmt.execute(statement);32 }33 }34 }35 public static void main(String[] args) throws SQLException {36 Connection con = ContainerDatabaseDriver.connect(url, null);37 String script = "CREATE TABLE test_table (\r38 " name VARCHAR(50) NOT NULL\r39 ");\r40 "INSERT INTO test_table (id, name) VALUES (1, 'John');\r41 "INSERT INTO test_table (id, name) VALUES (2, 'Paul');\r42 "INSERT INTO test_table (id, name) VALUES (3, 'George');\r43 "INSERT INTO test_table (id, name) VALUES (4, 'Ringo');\r44 "INSERT INTO test_table (id, name) VALUES (5, 'Pete');\r45 "INSERT INTO test_table (id, name) VALUES (6, 'Stu');\r

Full Screen

Full Screen

ScriptStatementFailedException

Using AI Code Generation

copy

Full Screen

1import org.testcontainers.containers.PostgreSQLContainer;2import org.testcontainers.ext.ScriptUtils;3import java.io.IOException;4import java.sql.Connection;5import java.sql.DriverManager;6import java.sql.SQLException;7public class Test {8 public static void main(String[] args) throws IOException, SQLException {9 PostgreSQLContainer container = new PostgreSQLContainer("postgres:11.1");10 container.start();11 String jdbcUrl = container.getJdbcUrl();12 String username = container.getUsername();13 String password = container.getPassword();14 Connection connection = DriverManager.getConnection(jdbcUrl, username, password);15 try {16 ScriptUtils.runScript(connection, "src/main/resources/db_scripts/create_table.sql");17 System.out.println("Script executed successfully");18 } catch (ScriptUtils.ScriptStatementFailedException e) {19 System.out.println(e.getMessage());20 }21 connection.close();22 container.stop();23 }24}25CREATE TABLE IF NOT EXISTS Student (26 name VARCHAR(100) NOT NULL,27);28INSERT INTO Student(id, name, age) VALUES (1, 'John', 20);29INSERT INTO Student(id, name, age) VALUES (2, 'Mary', 21);30import org.testcontainers.containers.MySQLContainer;31import org.testcontainers.ext.ScriptUtils;32import java.io.IOException;33import java.sql.Connection;34import java.sql.DriverManager;35import java.sql.SQLException;36public class Test {37 public static void main(String[] args) throws IOException, SQLException {38 MySQLContainer container = new MySQLContainer("mysql:8.0.18");39 container.start();40 String jdbcUrl = container.getJdbcUrl();41import org.testcontainers.jdbc.JdbcDatabaseContainer;42public class ScriptUtils {43 public static void execute(JdbcDatabaseContainer<?> container, String scriptPath) throws SQLException, ScriptStatementFailedException {44 Path script = Paths.get(scriptPath);45 try (Stream<String> lines = Files.lines(script, StandardCharsets.UTF_8)) {46 lines.forEach(line -> {47 try {48 container.execute(line);49 } catch (SQLException e) {50 throw new ScriptStatementFailedException("Failed to execute statement: " + line, e);51 }52 });53 } catch (IOException e) {54 throw new RuntimeException("Failed to read script file: " + scriptPath, e);55 }56 }57}58package org.testcontainers.ext;59public class ScriptStatementFailedException extends RuntimeException {60 public ScriptStatementFailedException(String message, Throwable cause) {61 super(message, cause);62 }63}64package org.testcontainers.ext;65public class ScriptUtilsTest {66 public static void main(String[] args) {67 }68}69package org.testcontainers.ext;70import java.sql.SQLException;71import org.testcontainers.containers.MySQLContainer;72public class ScriptUtilsTest {73 public static void main(String[] args) throws SQLException, ScriptStatementFailedException {74 try (MySQLContainer<?> container = new MySQLContainer<>()) {75 container.start();76 ScriptUtils.execute(container, "src/test/resources/init.sql");77 }78 }79}80CREATE TABLE IF NOT EXISTS test_table (id INT PRIMARY KEY);81INSERT INTO test_table VALUES (1);82INSERT INTO test_table VALUES (2);83INSERT INTO test_table VALUES (3);84INSERT INTO test_table VALUES (4);85INSERT INTO test_table VALUES (5);

Full Screen

Full Screen

ScriptStatementFailedException

Using AI Code Generation

copy

Full Screen

1public static void main(String[] args) throws Exception {2 try (Connection connection = DriverManager.getConnection("jdbc:h2:mem:db;DB_CLOSE_DELAY=-1", "sa", "")) {3 ScriptUtils.executeSqlScript(connection, new ClassPathResource("1.sql"));4 } catch (ScriptStatementFailedException e) {5 System.out.println(e.getStatement());6 }7}8CREATE TABLE TEST (ID INT PRIMARY KEY, NAME VARCHAR(255));9INSERT INTO TEST VALUES (1, 'TEST');10INSERT INTO TEST VALUES (1, 'TEST')

Full Screen

Full Screen

ScriptStatementFailedException

Using AI Code Generation

copy

Full Screen

1package org.testcontainers.ext;2import java.sql.Connection;3import java.sql.SQLException;4import java.sql.Statement;5import java.util.ArrayList;6import java.util.List;7import org.testcontainers.jdbc.ConnectionUrl;8import org.testcontainers.jdbc.ContainerDatabaseDriver;9public class ScriptUtils {10 public static void executeScript(Connection connection, String script) throws SQLException {11 List<String> statements = new ArrayList<>();12 StringBuilder currentStatement = new StringBuilder();13 String[] lines = script.split("\r14");15 for (String line : lines) {16 line = line.trim();17 if (line.startsWith("--")) {18 continue;19 }20 if (line.endsWith(";")) {21 currentStatement.append(line, 0, line.length() - 1);22 statements.add(currentStatement.toString());23 currentStatement = new StringBuilder();24 } else {25 currentStatement.append(line).append("26");27 }28 }29 for (String statement : statements) {30 try (Statement stmt = connection.createStatement()) {31 stmt.execute(statement);32 }33 }34 }35 public static void main(String[] args) throws SQLException {36 Connection con = ContainerDatabaseDriver.connect(url, null);37 String script = "CREATE TABLE test_table (\r38 " name VARCHAR(50) NOT NULL\r39 ");\r40 "INSERT INTO test_table (id, name) VALUES (1, 'John');\r41 "INSERT INTO test_table (id, name) VALUES (2, 'Paul');\r42 "INSERT INTO test_table (id, name) VALUES (3, 'George');\r43 "INSERT INTO test_table (id, name) VALUES (4, 'Ringo');\r44 "INSERT INTO test_table (id, name) VALUES (5, 'Pete');\r45 "INSERT INTO test_table (id, name) VALUES (6, 'Stu');\r

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful