This commit is contained in:
pivovarit 2017-05-15 18:19:33 +03:00
parent d46af17af4
commit 5a58aacb8f
3 changed files with 132 additions and 124 deletions

View File

@ -1,30 +1,28 @@
package com.baeldung.hikaricp; 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.HikariConfig;
import com.zaxxer.hikari.HikariDataSource; import com.zaxxer.hikari.HikariDataSource;
import java.sql.Connection;
import java.sql.SQLException;
public class DataSource { public class DataSource {
private static HikariConfig config = new HikariConfig(); private static HikariConfig config = new HikariConfig();
private static HikariDataSource ds; private static HikariDataSource ds;
static { static {
// config = new HikariConfig("datasource.properties"); /* config = new HikariConfig("datasource.properties");
// Properties props = new Properties(); Properties props = new Properties();
// props.setProperty("dataSourceClassName", "org.postgresql.ds.PGSimpleDataSource"); props.setProperty("dataSourceClassName", "org.postgresql.ds.PGSimpleDataSource");
// props.setProperty("dataSource.user", "postgres"); props.setProperty("dataSource.user", "postgres");
// props.setProperty("dataSource.password", "postgres"); props.setProperty("dataSource.password", "postgres");
// props.setProperty("dataSource.databaseName", "postgres"); props.setProperty("dataSource.databaseName", "postgres");
// props.setProperty("dataSource.portNumber", "5432"); props.setProperty("dataSource.portNumber", "5432");
// props.setProperty("dataSource.serverName", "localhost"); props.setProperty("dataSource.serverName", "localhost");
// props.put("dataSource.logWriter", new PrintWriter(System.out)); props.put("dataSource.logWriter", new PrintWriter(System.out));
// config = new HikariConfig(props); config = new HikariConfig(props);*/
config.setJdbcUrl("jdbc:postgresql://localhost:5432/postgres"); config.setJdbcUrl("jdbc:postgresql://localhost:5432/postgres");
config.setUsername("postgres"); config.setUsername("postgres");
@ -34,12 +32,13 @@ public class DataSource {
config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048"); config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
ds = new HikariDataSource(config); ds = new HikariDataSource(config);
// ds.setJdbcUrl("jdbc:postgresql://localhost:5432/postgres"); /* ds.setJdbcUrl("jdbc:postgresql://localhost:5432/postgres");
// ds.setUsername("postgres"); ds.setUsername("postgres");
// ds.setPassword("postgres"); ds.setPassword("postgres");*/
} }
private DataSource() {} private DataSource() {
}
public static Connection getConnection() throws SQLException { public static Connection getConnection() throws SQLException {
return ds.getConnection(); return ds.getConnection();

View File

@ -16,12 +16,15 @@ public class Employee {
public int getEmpNo() { public int getEmpNo() {
return empNo; return empNo;
} }
public void setEmpNo(int empNo) { public void setEmpNo(int empNo) {
this.empNo = empNo; this.empNo = empNo;
} }
public String getEname() { public String getEname() {
return ename; return ename;
} }
public void setEname(String ename) { public void setEname(String ename) {
this.ename = ename; this.ename = ename;
} }
@ -29,44 +32,54 @@ public class Employee {
public String getJob() { public String getJob() {
return job; return job;
} }
public void setJob(String job) { public void setJob(String job) {
this.job = job; this.job = job;
} }
public int getMgr() { public int getMgr() {
return mgr; return mgr;
} }
public void setMgr(int mgr) { public void setMgr(int mgr) {
this.mgr = mgr; this.mgr = mgr;
} }
public Date getHiredate() { public Date getHiredate() {
return hiredate; return hiredate;
} }
public void setHiredate(Date hiredate) { public void setHiredate(Date hiredate) {
this.hiredate = hiredate; this.hiredate = hiredate;
} }
public int getSal() { public int getSal() {
return sal; return sal;
} }
public void setSal(int sal) { public void setSal(int sal) {
this.sal = sal; this.sal = sal;
} }
public int getComm() { public int getComm() {
return comm; return comm;
} }
public void setComm(int comm) { public void setComm(int comm) {
this.comm = comm; this.comm = comm;
} }
public int getDeptno() { public int getDeptno() {
return deptno; return deptno;
} }
public void setDeptno(int deptno) { public void setDeptno(int deptno) {
this.deptno = deptno; this.deptno = deptno;
} }
@Override @Override
public String toString() { public String toString() {
return "Employee [empNo=" + empNo + ", ename=" + ename + ", job=" + job + ", mgr=" + mgr + ", hiredate=" 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);
+ hiredate + ", sal=" + sal + ", comm=" + comm + ", deptno=" + deptno + "]";
} }
} }

View File

@ -9,13 +9,11 @@ import java.util.List;
public class HikariCPDemo { public class HikariCPDemo {
public static List<Employee> fetchData() { public static List<Employee> fetchData() throws SQLException {
final String SQL_QUERY = "select * from emp"; final String SQL_QUERY = "select * from emp";
List<Employee> employees = null; List<Employee> employees;
try (Connection con = DataSource.getConnection(); try (Connection con = DataSource.getConnection(); PreparedStatement pst = con.prepareStatement(SQL_QUERY); ResultSet rs = pst.executeQuery()) {
PreparedStatement pst = con.prepareStatement(SQL_QUERY); employees = new ArrayList<>();
ResultSet rs = pst.executeQuery();) {
employees = new ArrayList<Employee>();
Employee employee; Employee employee;
while (rs.next()) { while (rs.next()) {
employee = new Employee(); employee = new Employee();
@ -29,8 +27,6 @@ public class HikariCPDemo {
employee.setDeptno(rs.getInt("deptno")); employee.setDeptno(rs.getInt("deptno"));
employees.add(employee); employees.add(employee);
} }
} catch (SQLException e) {
e.printStackTrace();
} }
return employees; return employees;
} }