diff --git a/core-java/pom.xml b/core-java/pom.xml index 40cc43fc09..ca4a363687 100644 --- a/core-java/pom.xml +++ b/core-java/pom.xml @@ -1,328 +1,413 @@ - 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 - - com.baeldung - parent-modules - 1.0.0-SNAPSHOT - + - + + + 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.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} - + - - org.projectlombok - lombok - ${lombok.version} - provided - + + 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 + - - - mysql - mysql-connector-java - ${mysql.version} - + - + + org.hamcrest + hamcrest-all + 1.3 + test + - - org.assertj - assertj-core - ${assertj.version} - test - + + junit + junit + ${junit.version} + test + - - com.jayway.awaitility - awaitility - ${avaitility.version} - test - + + org.hamcrest + hamcrest-core + ${org.hamcrest.version} + test + + + org.hamcrest + hamcrest-library + ${org.hamcrest.version} + test + - - commons-codec - commons-codec - ${commons-codec.version} - + + org.assertj + assertj-core + ${assertj.version} + test + - - org.javamoney - moneta - 1.1 - - - org.owasp.esapi - esapi - 2.1.0.1 - - - commons-logging - commons-logging - - - + + org.testng + testng + ${testng.version} + test + - - org.owasp.esapi - esapi - 2.1.0.1 - - - commons-logging - commons-logging - - - + + org.mockito + mockito-core + ${mockito.version} + test + + + com.jayway.awaitility + awaitility + ${avaitility.version} + test + - + + commons-codec + commons-codec + ${commons-codec.version} + - - core-java - - - src/main/resources - true - - + + org.javamoney + moneta + 1.1 + - - - org.apache.maven.plugins - maven-dependency-plugin - - - copy-dependencies - prepare-package - - copy-dependencies - - - ${project.build.directory}/libs - - - - + + org.owasp.esapi + esapi + 2.1.0.1 + - - org.apache.maven.plugins - maven-jar-plugin - - - - true - libs/ - org.baeldung.executable.ExecutableMavenJar - - - - - - org.apache.maven.plugins - maven-assembly-plugin - - - package - - single - - - ${project.basedir} - - - org.baeldung.executable.ExecutableMavenJar - - - - jar-with-dependencies - - - - - - - org.apache.maven.plugins - maven-shade-plugin - - - - shade - - - true - - - 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 - - - - - + + com.zaxxer + HikariCP + 2.6.1 + compile + - + + org.postgresql + postgresql + 42.0.0 + - - - integration - - - - org.apache.maven.plugins - maven-surefire-plugin - - - integration-test - - test - - - - **/*ManualTest.java - - - **/*IntegrationTest.java - - - - - - - json - - - - - - - + - - - 2.8.5 + + core-java + + + src/main/resources + true + + - - 6.0.6 + - - 21.0 - 3.5 - 1.55 - 1.10 - 3.6.1 - 2.5 - 4.1 - 4.01 - 0.4 - 1.8.7 - 1.16.12 + + org.apache.maven.plugins + maven-compiler-plugin + ${maven-compiler-plugin.version} + + 1.8 + 1.8 + + - - 3.6.1 - 1.7.0 + + org.apache.maven.plugins + maven-surefire-plugin + + + **/*IntegrationTest.java + **/*LongRunningUnitTest.java + **/*ManualTest.java + + true + + - + + 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-assembly-plugin + + + package + + single + + + ${project.basedir} + + + org.baeldung.executable.ExecutableMavenJar + + + + jar-with-dependencies + + + + + + + + org.apache.maven.plugins + maven-shade-plugin + + + + shade + + + true + + + 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 + + + + + + + + + + + + integration + + + + org.apache.maven.plugins + maven-surefire-plugin + + + integration-test + + test + + + + **/*ManualTest.java + + + **/*IntegrationTest.java + + + + + + + json + + + + + + + + + + + 2.8.5 + + + 1.7.21 + 1.1.7 + + + 21.0 + 3.5 + 1.55 + 1.10 + 3.6.1 + 2.5 + 4.1 + 4.01 + 0.4 + 1.8.7 + 1.16.12 + + + 1.3 + 4.12 + 1.10.19 + 6.10 + 3.6.1 + 1.7.0 + + + 3.6.0 + 2.19.1 + + + + \ No newline at end of file diff --git a/core-java/src/main/java/com/baeldung/hikaricp/DataSource.java b/core-java/src/main/java/com/baeldung/hikaricp/DataSource.java new file mode 100644 index 0000000000..996f528172 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/hikaricp/DataSource.java @@ -0,0 +1,48 @@ +package com.baeldung.hikaricp; + +import java.io.PrintWriter; +import java.sql.Connection; +import java.sql.SQLException; +import java.util.Properties; + +import com.zaxxer.hikari.HikariConfig; +import com.zaxxer.hikari.HikariDataSource; + +public class DataSource { + + private static HikariConfig config = new HikariConfig(); + private static HikariDataSource ds; + + static { +// config = new HikariConfig("datasource.properties"); + +// Properties props = new Properties(); +// props.setProperty("dataSourceClassName", "org.postgresql.ds.PGSimpleDataSource"); +// props.setProperty("dataSource.user", "postgres"); +// props.setProperty("dataSource.password", "postgres"); +// props.setProperty("dataSource.databaseName", "postgres"); +// props.setProperty("dataSource.portNumber", "5432"); +// props.setProperty("dataSource.serverName", "localhost"); +// props.put("dataSource.logWriter", new PrintWriter(System.out)); +// config = new HikariConfig(props); + + config.setJdbcUrl("jdbc:postgresql://localhost:5432/postgres"); + config.setUsername("postgres"); + config.setPassword("postgres"); + config.addDataSourceProperty("cachePrepStmts", "true"); + config.addDataSourceProperty("prepStmtCacheSize", "250"); + config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048"); + ds = new HikariDataSource(config); + +// ds.setJdbcUrl("jdbc:postgresql://localhost:5432/postgres"); +// ds.setUsername("postgres"); +// ds.setPassword("postgres"); + } + + private DataSource() {} + + public static Connection getConnection() throws SQLException { + return ds.getConnection(); + } + +} diff --git a/core-java/src/main/java/com/baeldung/hikaricp/Employee.java b/core-java/src/main/java/com/baeldung/hikaricp/Employee.java new file mode 100644 index 0000000000..5472d5c18c --- /dev/null +++ b/core-java/src/main/java/com/baeldung/hikaricp/Employee.java @@ -0,0 +1,72 @@ +package com.baeldung.hikaricp; + +import java.sql.Date; + +public class Employee { + + private int empNo; + private String ename; + private String job; + private int mgr; + private Date hiredate; + private int sal; + private int comm; + private int deptno; + + public int getEmpNo() { + return empNo; + } + public void setEmpNo(int empNo) { + this.empNo = empNo; + } + public String getEname() { + return ename; + } + public void setEname(String ename) { + this.ename = ename; + } + + public String getJob() { + return job; + } + public void setJob(String job) { + this.job = job; + } + public int getMgr() { + return mgr; + } + public void setMgr(int mgr) { + this.mgr = mgr; + } + public Date getHiredate() { + return hiredate; + } + public void setHiredate(Date hiredate) { + this.hiredate = hiredate; + } + public int getSal() { + return sal; + } + public void setSal(int sal) { + this.sal = sal; + } + public int getComm() { + return comm; + } + public void setComm(int comm) { + this.comm = comm; + } + public int getDeptno() { + return deptno; + } + public void setDeptno(int deptno) { + this.deptno = deptno; + } + + @Override + public String toString() { + return "Employee [empNo=" + empNo + ", ename=" + ename + ", job=" + job + ", mgr=" + mgr + ", hiredate=" + + hiredate + ", sal=" + sal + ", comm=" + comm + ", deptno=" + deptno + "]"; + } + +} diff --git a/core-java/src/main/java/com/baeldung/hikaricp/HikariCPDemo.java b/core-java/src/main/java/com/baeldung/hikaricp/HikariCPDemo.java new file mode 100644 index 0000000000..97b5bf5644 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/hikaricp/HikariCPDemo.java @@ -0,0 +1,38 @@ +package com.baeldung.hikaricp; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; + +public class HikariCPDemo { + + public static List fetchData() { + final String SQL_QUERY = "select * from emp"; + List employees = null; + try (Connection con = DataSource.getConnection(); + PreparedStatement pst = con.prepareStatement(SQL_QUERY); + ResultSet rs = pst.executeQuery();) { + employees = new ArrayList(); + Employee employee; + while (rs.next()) { + employee = new Employee(); + employee.setEmpNo(rs.getInt("empno")); + employee.setEname(rs.getString("ename")); + employee.setJob(rs.getString("job")); + employee.setMgr(rs.getInt("mgr")); + employee.setHiredate(rs.getDate("hiredate")); + employee.setSal(rs.getInt("sal")); + employee.setComm(rs.getInt("comm")); + employee.setDeptno(rs.getInt("deptno")); + employees.add(employee); + } + } catch (SQLException e) { + e.printStackTrace(); + } + return employees; + } + +} diff --git a/core-java/src/main/java/com/baeldung/hikaricp/db.sql b/core-java/src/main/java/com/baeldung/hikaricp/db.sql new file mode 100644 index 0000000000..1dac59307b --- /dev/null +++ b/core-java/src/main/java/com/baeldung/hikaricp/db.sql @@ -0,0 +1,47 @@ +create table dept( + deptno numeric, + dname varchar(14), + loc varchar(13), + constraint pk_dept primary key (deptno) +); + +create table emp( + empno numeric, + ename varchar(10), + job varchar(9), + mgr numeric, + hiredate date, + sal numeric, + comm numeric, + deptno numeric, + constraint pk_emp primary key (empno), + constraint fk_deptno foreign key (deptno) references dept (deptno) +); + +insert into dept values(10, 'ACCOUNTING', 'NEW YORK'); +insert into dept values(20, 'RESEARCH', 'DALLAS'); +insert into dept values(30, 'SALES', 'CHICAGO'); +insert into dept values(40, 'OPERATIONS', 'BOSTON'); + +insert into emp values( + 7839, 'KING', 'PRESIDENT', null, + to_date('17-11-1981','dd-mm-yyyy'), + 7698, null, 10 +); +insert into emp values( + 7698, 'BLAKE', 'MANAGER', 7839, + to_date('1-5-1981','dd-mm-yyyy'), + 7782, null, 20 +); +insert into emp values( + 7782, 'CLARK', 'MANAGER', 7839, + to_date('9-6-1981','dd-mm-yyyy'), + 7566, null, 30 +); +insert into emp values( + 7566, 'JONES', 'MANAGER', 7839, + to_date('2-4-1981','dd-mm-yyyy'), + 7839, null, 40 +); + +commit; \ No newline at end of file diff --git a/core-java/src/main/resources/datasource.properties b/core-java/src/main/resources/datasource.properties new file mode 100644 index 0000000000..61df0d45f7 --- /dev/null +++ b/core-java/src/main/resources/datasource.properties @@ -0,0 +1,6 @@ +dataSourceClassName=//TBD +dataSource.user=//TBD +dataSource.password=//TBD +dataSource.databaseName=//TBD +dataSource.portNumber=//TBD +dataSource.serverName=//TBD \ No newline at end of file diff --git a/core-java/src/test/java/com/baeldung/hikaricp/HikariCPTest.java b/core-java/src/test/java/com/baeldung/hikaricp/HikariCPTest.java new file mode 100644 index 0000000000..5504d8b99e --- /dev/null +++ b/core-java/src/test/java/com/baeldung/hikaricp/HikariCPTest.java @@ -0,0 +1,19 @@ +package com.baeldung.hikaricp; + +import java.util.List; + +import org.junit.Ignore; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class HikariCPTest { + + @Test +// @Ignore + public void givenConnection_thenFetchDbData() { + List employees = HikariCPDemo.fetchData(); + assertEquals(4, employees.size()); + } + +}