diff --git a/core-java/pom.xml b/core-java/pom.xml index 068a772c43..88ac6bca49 100644 --- a/core-java/pom.xml +++ b/core-java/pom.xml @@ -1,496 +1,495 @@ - 4.0.0 - com.baeldung - core-java - 0.1.0-SNAPSHOT - jar + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + 4.0.0 + com.baeldung + core-java + 0.1.0-SNAPSHOT + jar - core-java + core-java - + - - - net.sourceforge.collections - collections-generic - ${collections-generic.version} - - - com.google.guava - guava - ${guava.version} - + + + net.sourceforge.collections + collections-generic + ${collections-generic.version} + + + com.google.guava + guava + ${guava.version} + - - org.apache.commons - commons-collections4 - ${commons-collections4.version} - + + org.apache.commons + commons-collections4 + ${commons-collections4.version} + - - commons-io - commons-io - ${commons-io.version} - + + commons-io + commons-io + ${commons-io.version} + - - org.apache.commons - commons-lang3 - ${commons-lang3.version} - + + org.apache.commons + commons-lang3 + ${commons-lang3.version} + - - org.apache.commons - commons-math3 - ${commons-math3.version} - + + org.apache.commons + commons-math3 + ${commons-math3.version} + - - org.decimal4j - decimal4j - ${decimal4j.version} - + + org.decimal4j + decimal4j + ${decimal4j.version} + - - org.bouncycastle - bcprov-jdk15on - ${bouncycastle.version} - + + org.bouncycastle + bcprov-jdk15on + ${bouncycastle.version} + - - org.unix4j - unix4j-command - ${unix4j.version} - + + org.unix4j + unix4j-command + ${unix4j.version} + - - com.googlecode.grep4j - grep4j - ${grep4j.version} - - + + com.googlecode.grep4j + grep4j + ${grep4j.version} + + - + - - com.fasterxml.jackson.core - jackson-databind - ${jackson.version} - + + com.fasterxml.jackson.core + jackson-databind + ${jackson.version} + - - - log4j - log4j - 1.2.17 - - - org.slf4j - slf4j-api - ${org.slf4j.version} - - - ch.qos.logback - logback-classic - ${logback.version} - - - - org.slf4j - jcl-over-slf4j - ${org.slf4j.version} - - - - org.slf4j - log4j-over-slf4j - ${org.slf4j.version} - - - org.projectlombok - lombok - ${lombok.version} - provided - + + + log4j + log4j + 1.2.17 + + + org.slf4j + slf4j-api + ${org.slf4j.version} + + + ch.qos.logback + logback-classic + ${logback.version} + + + + org.slf4j + jcl-over-slf4j + ${org.slf4j.version} + + + + org.slf4j + log4j-over-slf4j + ${org.slf4j.version} + + + org.projectlombok + lombok + ${lombok.version} + provided + - + - - org.hamcrest - hamcrest-all - 1.3 - test - + + org.hamcrest + hamcrest-all + 1.3 + test + - - junit - junit - ${junit.version} - test - + + junit + junit + ${junit.version} + test + - - org.hamcrest - hamcrest-core - ${org.hamcrest.version} - test - - - org.hamcrest - hamcrest-library - ${org.hamcrest.version} - test - + + org.hamcrest + hamcrest-core + ${org.hamcrest.version} + test + + + org.hamcrest + hamcrest-library + ${org.hamcrest.version} + test + - - org.assertj - assertj-core - ${assertj.version} - test - + + org.assertj + assertj-core + ${assertj.version} + test + - - org.mockito - mockito-core - ${mockito.version} - test - - - com.jayway.awaitility - awaitility - ${avaitility.version} - test - + + org.mockito + mockito-core + ${mockito.version} + test + + + com.jayway.awaitility + awaitility + ${avaitility.version} + test + - - commons-codec - commons-codec - ${commons-codec.version} - + + commons-codec + commons-codec + ${commons-codec.version} + - - org.javamoney - moneta - 1.1 - + + org.javamoney + moneta + 1.1 + - - org.owasp.esapi - esapi - 2.1.0.1 - - - com.h2database - h2 - 1.4.196 - runtime - - - com.sun.messaging.mq - fscontext - ${fscontext.version} - - - com.codepoetics - protonpack - ${protonpack.version} - - - one.util - streamex - ${streamex.version} - - - io.vavr - vavr - ${vavr.version} - - - org.openjdk.jmh - jmh-core - 1.19 - - - org.openjdk.jmh - jmh-generator-annprocess - 1.19 - - - org.springframework - spring-web - 4.3.4.RELEASE - + + org.owasp.esapi + esapi + 2.1.0.1 + + + com.sun.messaging.mq + fscontext + ${fscontext.version} + + + com.codepoetics + protonpack + ${protonpack.version} + + + one.util + streamex + ${streamex.version} + + + io.vavr + vavr + ${vavr.version} + + + org.openjdk.jmh + jmh-core + 1.19 + + + org.openjdk.jmh + jmh-generator-annprocess + 1.19 + + org.springframework + spring-web + 4.3.4.RELEASE + + org.springframework.boot spring-boot-starter 1.5.8.RELEASE + + org.hsqldb + hsqldb + 2.4.0 + runtime + + - + + core-java + + + src/main/resources + true + + - - core-java - - - src/main/resources - true - - + - + + org.apache.maven.plugins + maven-compiler-plugin + ${maven-compiler-plugin.version} + + 1.8 + 1.8 + + - - org.apache.maven.plugins - maven-compiler-plugin - ${maven-compiler-plugin.version} - - 1.8 - 1.8 - - + + org.apache.maven.plugins + maven-surefire-plugin + + + **/*LiveTest.java + **/*IntegrationTest.java + **/*LongRunningUnitTest.java + **/*ManualTest.java + + true + + - - org.apache.maven.plugins - maven-surefire-plugin - - - **/*LiveTest.java - **/*IntegrationTest.java - **/*LongRunningUnitTest.java - **/*ManualTest.java - + + org.apache.maven.plugins + maven-dependency-plugin + + + copy-dependencies + prepare-package + + copy-dependencies + + + ${project.build.directory}/libs + + + + - - + + org.apache.maven.plugins + maven-jar-plugin + + + + true + libs/ + org.baeldung.executable.ExecutableMavenJar + + + + - - org.apache.maven.plugins - maven-dependency-plugin - - - copy-dependencies - prepare-package - - copy-dependencies - - - ${project.build.directory}/libs - - - - + + org.apache.maven.plugins + maven-assembly-plugin + + + package + + single + + + ${project.basedir} + + + org.baeldung.executable.ExecutableMavenJar + + + + jar-with-dependencies + + + + + - - org.apache.maven.plugins - maven-jar-plugin - - - - true - libs/ - org.baeldung.executable.ExecutableMavenJar - - - - + + org.apache.maven.plugins + maven-shade-plugin + + + + shade + + + true + + + org.baeldung.executable.ExecutableMavenJar + + + + + + - - org.apache.maven.plugins - maven-assembly-plugin - - - package - - single - - - ${project.basedir} - - - org.baeldung.executable.ExecutableMavenJar - - - - jar-with-dependencies - - - - - + + com.jolira + onejar-maven-plugin + + + + org.baeldung.executable.ExecutableMavenJar + true + ${project.build.finalName}-onejar.${project.packaging} + + + one-jar + + + + - - org.apache.maven.plugins - maven-shade-plugin - - - - shade - - - true - - - org.baeldung.executable.ExecutableMavenJar - - - - - - + + org.springframework.boot + spring-boot-maven-plugin + + + + repackage + + + spring-boot + org.baeldung.executable.ExecutableMavenJar + + + + - - com.jolira - onejar-maven-plugin - - - - org.baeldung.executable.ExecutableMavenJar - true - ${project.build.finalName}-onejar.${project.packaging} - - - one-jar - - - - - - - org.springframework.boot - spring-boot-maven-plugin - - - - repackage - - - spring-boot - org.baeldung.executable.ExecutableMavenJar - - - - - - - org.codehaus.mojo - exec-maven-plugin - 1.6.0 - - java - com.baeldung.outofmemoryerror.OutOfMemoryGCLimitExceed - - -Xmx300m - -XX:+UseParallelGC - -classpath - - com.baeldung.outofmemoryerror.OutOfMemoryGCLimitExceed - - - + + org.codehaus.mojo + exec-maven-plugin + 1.6.0 + + java + com.baeldung.outofmemoryerror.OutOfMemoryGCLimitExceed + + -Xmx300m + -XX:+UseParallelGC + -classpath + + com.baeldung.outofmemoryerror.OutOfMemoryGCLimitExceed + + + - + - + - - - integration - - - - org.apache.maven.plugins - maven-surefire-plugin - - - integration-test - - test - - - - **/*ManualTest.java - - - **/*IntegrationTest.java - - - - - - - json - - - - - org.codehaus.mojo - exec-maven-plugin + + + integration + + + + org.apache.maven.plugins + maven-surefire-plugin + + + integration-test + + test + + + + **/*ManualTest.java + + + **/*IntegrationTest.java + + + + + + + json + + + + + org.codehaus.mojo + exec-maven-plugin - - - run-benchmarks - - none - - exec - - - test - java - - -classpath - - org.openjdk.jmh.Main - .* - - - - - - - - - + + + run-benchmarks + + none + + exec + + + test + java + + -classpath + + org.openjdk.jmh.Main + .* + + + + + + + + + - - - 2.8.5 + + + 2.8.5 - - 1.7.21 - 1.1.7 + + 1.7.21 + 1.1.7 - - 22.0 - 3.5 - 1.55 - 1.10 - 3.6.1 - 1.0.3 - 2.5 - 4.1 - 4.01 - 0.4 - 1.8.7 - 1.16.12 - 4.6-b01 - 1.13 - 0.6.5 - 0.9.0 + + 22.0 + 3.5 + 1.55 + 1.10 + 3.6.1 + 1.0.3 + 2.5 + 4.1 + 4.01 + 0.4 + 1.8.7 + 1.16.12 + 4.6-b01 + 1.13 + 0.6.5 + 0.9.0 + + + 1.3 + 4.12 + 2.8.9 + 3.6.1 + 1.7.0 - - 1.3 - 4.12 - 2.8.9 - 3.6.1 - 1.7.0 - - - 3.6.0 - 2.19.1 - - + + 3.6.0 + 2.19.1 + + \ No newline at end of file diff --git a/core-java/src/main/java/com/baeldung/jdbc/BatchProcessing.java b/core-java/src/main/java/com/baeldung/jdbc/BatchProcessing.java new file mode 100644 index 0000000000..d5c53c3ffc --- /dev/null +++ b/core-java/src/main/java/com/baeldung/jdbc/BatchProcessing.java @@ -0,0 +1,96 @@ +package com.baeldung.jdbc; + +import java.sql.*; +import java.util.UUID; + +public class BatchProcessing { + + private final String[] EMPLOYEES = new String[]{"Zuck","Mike","Larry","Musk","Steve"}; + private final String[] DESIGNATIONS = new String[]{"CFO","CSO","CTO","CEO","CMO"}; + private final String[] ADDRESSES = new String[]{"China","York","Diego","Carolina","India"}; + + private Connection connection; + + public void getConnection(){ + try { + Class.forName("org.hsqldb.jdbcDriver"); + connection = DriverManager.getConnection("jdbc:hsqldb:file:C:\\EMPLOYEEDB", "SA", ""); + connection.setAutoCommit(false); + } catch (Exception e) { + e.printStackTrace(System.out); + } + } + + public void createTables(){ + try { + connection.createStatement().executeUpdate("create table EMPLOYEE (ID VARCHAR(36), NAME VARCHAR(45), DESIGNATION VARCHAR(15))"); + connection.createStatement().executeUpdate("create table EMP_ADDRESS (ID VARCHAR(36), EMP_ID VARCHAR(36), ADDRESS VARCHAR(45))"); + System.out.println("Tables Created!!!"); + } catch (SQLException e) { + e.printStackTrace(System.out); + } + } + + public void useStatement(){ + try { + String insertEmployeeSQL = "INSERT INTO EMPLOYEE(ID, NAME, DESIGNATION) VALUES ('%s','%s','%s');"; + String insertEmployeeAddrSQL = "INSERT INTO EMP_ADDRESS(ID, EMP_ID, ADDRESS) VALUES ('%s','%s','%s');"; + Statement statement = connection.createStatement(); + for(int i = 0; i < EMPLOYEES.length; i++){ + String employeeId = UUID.randomUUID().toString(); + statement.addBatch(String.format(insertEmployeeSQL, employeeId, EMPLOYEES[i],DESIGNATIONS[i])); + statement.addBatch(String.format(insertEmployeeAddrSQL, UUID.randomUUID().toString(),employeeId,ADDRESSES[i])); + } + statement.executeBatch(); + connection.commit(); + } catch (Exception e) { + try { + connection.rollback(); + } catch (SQLException ex) { + System.out.println("Error during rollback"); + System.out.println(ex.getMessage()); + } + e.printStackTrace(System.out); + } + } + + public void usePreparedStatement(){ + try { + String insertEmployeeSQL = "INSERT INTO EMPLOYEE(ID, NAME, DESIGNATION) VALUES (?,?,?);"; + String insertEmployeeAddrSQL = "INSERT INTO EMP_ADDRESS(ID, EMP_ID, ADDRESS) VALUES (?,?,?);"; + PreparedStatement employeeStmt = connection.prepareStatement(insertEmployeeSQL); + PreparedStatement empAddressStmt = connection.prepareStatement(insertEmployeeAddrSQL); + for(int i = 0; i < EMPLOYEES.length; i++){ + String employeeId = UUID.randomUUID().toString(); + employeeStmt.setString(1,employeeId); + employeeStmt.setString(2,EMPLOYEES[i]); + employeeStmt.setString(3,DESIGNATIONS[i]); + employeeStmt.addBatch(); + + empAddressStmt.setString(1,UUID.randomUUID().toString()); + empAddressStmt.setString(2,employeeId); + empAddressStmt.setString(3,ADDRESSES[i]); + empAddressStmt.addBatch(); + } + employeeStmt.executeBatch(); + empAddressStmt.executeBatch(); + connection.commit(); + } catch (Exception e) { + try { + connection.rollback(); + } catch (SQLException ex) { + System.out.println("Error during rollback"); + System.out.println(ex.getMessage()); + } + e.printStackTrace(System.out); + } + } + + public static void main(String[] args) { + BatchProcessing batchProcessing = new BatchProcessing(); + batchProcessing.getConnection(); + batchProcessing.createTables(); + batchProcessing.useStatement(); + batchProcessing.usePreparedStatement(); + } +} diff --git a/core-java/src/test/java/com/baeldung/jdbc/BatchProcessingTest.java b/core-java/src/test/java/com/baeldung/jdbc/BatchProcessingTest.java new file mode 100644 index 0000000000..90f2ea133f --- /dev/null +++ b/core-java/src/test/java/com/baeldung/jdbc/BatchProcessingTest.java @@ -0,0 +1,71 @@ +package com.baeldung.jdbc; + + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.mockito.junit.MockitoJUnitRunner; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.Statement; + +@RunWith(MockitoJUnitRunner.class) +public class BatchProcessingTest { + + + @InjectMocks + private BatchProcessing target = new BatchProcessing(); + + @Mock + private Connection connection; + + @Mock + private Statement statement; + + @Mock + private PreparedStatement employeeStatement; + + @Mock + private PreparedStatement employeeAddressStatement; + + @Before + public void before(){ + MockitoAnnotations.initMocks(this); + } + + + @Test + public void when_useStatement_thenInsertData_success() throws Exception { + Mockito.when(connection.createStatement()).thenReturn(statement); + target.useStatement(); + } + + @Test + public void when_useStatement_ifThrowException_thenCatchException() throws Exception { + Mockito.when(connection.createStatement()).thenThrow(new RuntimeException()); + target.useStatement(); + } + + @Test + public void when_usePreparedStatement_thenInsertData_success() throws Exception { + String insertEmployeeSQL = "INSERT INTO EMPLOYEE(ID, NAME, DESIGNATION) VALUES (?,?,?);"; + String insertEmployeeAddrSQL = "INSERT INTO EMP_ADDRESS(ID, EMP_ID, ADDRESS) VALUES (?,?,?);"; + Mockito.when(connection.prepareStatement(insertEmployeeSQL)).thenReturn(employeeStatement); + Mockito.when(connection.prepareStatement(insertEmployeeAddrSQL)).thenReturn(employeeAddressStatement); + target.usePreparedStatement(); + } + + @Test + public void when_usePreparedStatement_ifThrowException_thenCatchException() throws Exception { + String insertEmployeeSQL = "INSERT INTO EMPLOYEE(ID, NAME, DESIGNATION) VALUES (?,?,?);"; + String insertEmployeeAddrSQL = "INSERT INTO EMP_ADDRESS(ID, EMP_ID, ADDRESS) VALUES (?,?,?);"; + Mockito.when(connection.prepareStatement(insertEmployeeSQL)).thenReturn(employeeStatement); + Mockito.when(connection.prepareStatement(insertEmployeeAddrSQL)).thenThrow(new RuntimeException()); + target.usePreparedStatement(); + } +}