From: Gustavo Martin Morcuende Date: Sun, 2 Oct 2016 21:05:39 +0000 (+0200) Subject: Using PreparedStatement X-Git-Url: https://git.gumartinm.name/?a=commitdiff_plain;h=8ad3358556f7e403dbea5862103e3fcfbdf92a32;p=WebAppTest%2F.git Using PreparedStatement --- diff --git a/src/main/java/com/prueba/core/context/integration/database/DataBaseAccess.java b/src/main/java/com/prueba/core/context/integration/database/DataBaseAccess.java index 4a47d0d..94cc2a9 100644 --- a/src/main/java/com/prueba/core/context/integration/database/DataBaseAccess.java +++ b/src/main/java/com/prueba/core/context/integration/database/DataBaseAccess.java @@ -1,15 +1,15 @@ package com.prueba.core.context.integration.database; +import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; -import java.sql.Statement; import java.util.List; import java.util.Map; public interface DataBaseAccess { public interface ExecuteStatement { - TResult executeStatement(final Statement statement) throws SQLException; + TResult executeStatement(final PreparedStatement statement) throws SQLException; } public interface ExecuteResultSet { diff --git a/src/main/java/com/prueba/core/context/integration/database/impl/DataBaseAccessImpl.java b/src/main/java/com/prueba/core/context/integration/database/impl/DataBaseAccessImpl.java index ca335a1..c97d563 100644 --- a/src/main/java/com/prueba/core/context/integration/database/impl/DataBaseAccessImpl.java +++ b/src/main/java/com/prueba/core/context/integration/database/impl/DataBaseAccessImpl.java @@ -1,6 +1,7 @@ package com.prueba.core.context.integration.database.impl; import java.sql.Connection; +import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; @@ -36,8 +37,9 @@ public class DataBaseAccessImpl implements DataBaseAccess { final Connection connection = dataSource.getConnection(); return this.doExecuteQuery( + query, connection, - statement -> statement.executeQuery(query), + preparedStatement -> preparedStatement.executeQuery(), executeResultSet ); } finally { @@ -45,12 +47,14 @@ public class DataBaseAccessImpl implements DataBaseAccess { } } - protected List> doExecuteQuery (final Connection connection, + protected List> doExecuteQuery ( + final String query, + final Connection connection, final ExecuteStatement executeStatement, final ExecuteResultSet executeResultSet) throws SQLException { - try (final Statement statement = connection.createStatement(); - final ResultSet answer = executeStatement.executeStatement(statement)) { + try (final PreparedStatement preparedStatement = connection.prepareStatement(query); + final ResultSet answer = executeStatement.executeStatement(preparedStatement)) { return executeResultSet.executeResultSet(answer); } diff --git a/src/main/java/com/prueba/core/context/web/application/ApplicationWebContext.java b/src/main/java/com/prueba/core/context/web/application/ApplicationWebContext.java new file mode 100644 index 0000000..9d2c7fe --- /dev/null +++ b/src/main/java/com/prueba/core/context/web/application/ApplicationWebContext.java @@ -0,0 +1,29 @@ +package com.prueba.core.web.application.context; + +import javax.sql.DataSource; + +import com.prueba.core.context.ApplicationContext; +import com.prueba.core.context.integration.datasource.impl.DoDataSourceContext; + + +public class ApplicationWebContext implements ApplicationContext { + private final DataSource dataSource; + + private ApplicationWebContext() { + this.dataSource = DoDataSourceContext.getInstance().getDataSource(); + } + + private static class ApplicationWebContextHolder { + private static final ApplicationContext INSTANCE = new ApplicationWebContext(); + } + + public static ApplicationContext getInstance() { + return ApplicationWebContextHolder.INSTANCE; + } + + @Override + public DataSource getDataSource() { + return this.dataSource; + } + +} diff --git a/src/main/java/com/prueba/core/web/application/context/ApplicationWebContext.java b/src/main/java/com/prueba/core/web/application/context/ApplicationWebContext.java deleted file mode 100644 index 9d2c7fe..0000000 --- a/src/main/java/com/prueba/core/web/application/context/ApplicationWebContext.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.prueba.core.web.application.context; - -import javax.sql.DataSource; - -import com.prueba.core.context.ApplicationContext; -import com.prueba.core.context.integration.datasource.impl.DoDataSourceContext; - - -public class ApplicationWebContext implements ApplicationContext { - private final DataSource dataSource; - - private ApplicationWebContext() { - this.dataSource = DoDataSourceContext.getInstance().getDataSource(); - } - - private static class ApplicationWebContextHolder { - private static final ApplicationContext INSTANCE = new ApplicationWebContext(); - } - - public static ApplicationContext getInstance() { - return ApplicationWebContextHolder.INSTANCE; - } - - @Override - public DataSource getDataSource() { - return this.dataSource; - } - -} diff --git a/src/test/java/com/prueba/core/context/integration/liquibase/impl/LiquibaseContextIntegrationTest.java b/src/test/java/com/prueba/core/context/integration/liquibase/impl/LiquibaseContextIntegrationTest.java index 06a3cd6..ceeb4bf 100644 --- a/src/test/java/com/prueba/core/context/integration/liquibase/impl/LiquibaseContextIntegrationTest.java +++ b/src/test/java/com/prueba/core/context/integration/liquibase/impl/LiquibaseContextIntegrationTest.java @@ -23,7 +23,7 @@ public class LiquibaseContextIntegrationTest { private static final String APP_ROLE_CODE = "APPLICATION_ROLE_CODE"; @Test - public void whenLoadLiquibaseContextThenReturnSuccessFulQuery() { + public void whenLoadLiquibaseContextThenReturnResultsFromDatabase() { final DataSource dataSource = DoDataSourceContext.getInstance().getDataSource(); final LiquibaseContext liquibaseContext = new LiquibaseContext(dataSource); liquibaseContext.init();