Using PreparedStatement
authorGustavo Martin Morcuende <gu.martinm@gmail.com>
Sun, 2 Oct 2016 21:05:39 +0000 (23:05 +0200)
committerGustavo Martin Morcuende <gu.martinm@gmail.com>
Sun, 2 Oct 2016 21:05:39 +0000 (23:05 +0200)
src/main/java/com/prueba/core/context/integration/database/DataBaseAccess.java
src/main/java/com/prueba/core/context/integration/database/impl/DataBaseAccessImpl.java
src/main/java/com/prueba/core/context/web/application/ApplicationWebContext.java [new file with mode: 0644]
src/main/java/com/prueba/core/web/application/context/ApplicationWebContext.java [deleted file]
src/test/java/com/prueba/core/context/integration/liquibase/impl/LiquibaseContextIntegrationTest.java

index 4a47d0d..94cc2a9 100644 (file)
@@ -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> {
-        TResult executeStatement(final Statement statement) throws SQLException;
+        TResult executeStatement(final PreparedStatement statement) throws SQLException;
     }
 
     public interface ExecuteResultSet<TResult> {
index ca335a1..c97d563 100644 (file)
@@ -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<Map<String, String>> doExecuteQuery (final Connection connection,
+       protected List<Map<String, String>> doExecuteQuery (
+                       final String query,
+                       final Connection connection,
             final ExecuteStatement<ResultSet> executeStatement,
             final ExecuteResultSet<ResultSet> 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 (file)
index 0000000..9d2c7fe
--- /dev/null
@@ -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 (file)
index 9d2c7fe..0000000
+++ /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;
-       }
-
-}
index 06a3cd6..ceeb4bf 100644 (file)
@@ -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();