From 5a58aacb8ff8edf3dcc8c80f0d9b3953b6aaa8d7 Mon Sep 17 00:00:00 2001 From: pivovarit Date: Mon, 15 May 2017 18:19:33 +0300 Subject: [PATCH] Refactor --- .../com/baeldung/hikaricp/DataSource.java | 71 +++++---- .../java/com/baeldung/hikaricp/Employee.java | 139 ++++++++++-------- .../com/baeldung/hikaricp/HikariCPDemo.java | 46 +++--- 3 files changed, 132 insertions(+), 124 deletions(-) diff --git a/libraries/src/main/java/com/baeldung/hikaricp/DataSource.java b/libraries/src/main/java/com/baeldung/hikaricp/DataSource.java index 996f528172..d3809dcedd 100644 --- a/libraries/src/main/java/com/baeldung/hikaricp/DataSource.java +++ b/libraries/src/main/java/com/baeldung/hikaricp/DataSource.java @@ -1,48 +1,47 @@ 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; +import java.sql.Connection; +import java.sql.SQLException; + public class DataSource { - private static HikariConfig config = new HikariConfig(); - private static HikariDataSource ds; + 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"); - } + static { + /* config = new HikariConfig("datasource.properties"); - private DataSource() {} + 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);*/ - public static Connection getConnection() throws SQLException { - return ds.getConnection(); - } + 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/libraries/src/main/java/com/baeldung/hikaricp/Employee.java b/libraries/src/main/java/com/baeldung/hikaricp/Employee.java index 5472d5c18c..1ea3c13a48 100644 --- a/libraries/src/main/java/com/baeldung/hikaricp/Employee.java +++ b/libraries/src/main/java/com/baeldung/hikaricp/Employee.java @@ -4,69 +4,82 @@ 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; + 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; - } + 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 String.format("Employee [empNo=%d, ename=%s, job=%s, mgr=%d, hiredate=%s, sal=%d, comm=%d, deptno=%d]", empNo, ename, job, mgr, hiredate, sal, comm, 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/libraries/src/main/java/com/baeldung/hikaricp/HikariCPDemo.java b/libraries/src/main/java/com/baeldung/hikaricp/HikariCPDemo.java index 97b5bf5644..014202ac10 100644 --- a/libraries/src/main/java/com/baeldung/hikaricp/HikariCPDemo.java +++ b/libraries/src/main/java/com/baeldung/hikaricp/HikariCPDemo.java @@ -9,30 +9,26 @@ 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; - } + public static List fetchData() throws SQLException { + final String SQL_QUERY = "select * from emp"; + List employees; + 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); + } + } + return employees; + } }