final version (#1867)
This commit is contained in:
committed by
Zeger Hendrikse
parent
3f22a70bb3
commit
74d4c4f72c
@@ -1,47 +1,45 @@
|
||||
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");
|
||||
static {
|
||||
// config = new HikariConfig("datasource.properties");
|
||||
|
||||
// Properties props = new Properties();
|
||||
// props.setProperty("dataSourceClassName", "org.h2.Driver");
|
||||
// props.setProperty("dataSource.user", "");
|
||||
// props.setProperty("dataSource.password", "");
|
||||
// props.put("dataSource.logWriter", new PrintWriter(System.out));
|
||||
// config = new HikariConfig(props);
|
||||
|
||||
config.setJdbcUrl("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;INIT=runscript from 'classpath:/db.sql'");
|
||||
config.setUsername("");
|
||||
config.setPassword("");
|
||||
config.addDataSourceProperty("cachePrepStmts", "true");
|
||||
config.addDataSourceProperty("prepStmtCacheSize", "250");
|
||||
config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
|
||||
ds = new HikariDataSource(config);
|
||||
|
||||
// ds.setJdbcUrl("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;INIT=runscript from 'classpath:/db.sql'");
|
||||
// ds.setUsername("");
|
||||
// ds.setPassword("");
|
||||
}
|
||||
|
||||
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);*/
|
||||
private DataSource() {}
|
||||
|
||||
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);
|
||||
public static Connection getConnection() throws SQLException {
|
||||
return ds.getConnection();
|
||||
}
|
||||
|
||||
/* ds.setJdbcUrl("jdbc:postgresql://localhost:5432/postgres");
|
||||
ds.setUsername("postgres");
|
||||
ds.setPassword("postgres");*/
|
||||
}
|
||||
|
||||
private DataSource() {
|
||||
}
|
||||
|
||||
public static Connection getConnection() throws SQLException {
|
||||
return ds.getConnection();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -9,26 +9,34 @@ import java.util.List;
|
||||
|
||||
public class HikariCPDemo {
|
||||
|
||||
public static List<Employee> fetchData() throws SQLException {
|
||||
final String SQL_QUERY = "select * from emp";
|
||||
List<Employee> 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;
|
||||
}
|
||||
public static List<Employee> fetchData() {
|
||||
final String SQL_QUERY = "select * from emp";
|
||||
List<Employee> employees = null;
|
||||
try (Connection con = DataSource.getConnection();
|
||||
PreparedStatement pst = con.prepareStatement(SQL_QUERY);
|
||||
ResultSet rs = pst.executeQuery();) {
|
||||
employees = new ArrayList<Employee>();
|
||||
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 void main(String[] args) {
|
||||
fetchData();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -2,19 +2,16 @@ package com.baeldung.hikaricp;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class HikariCPUnitTest {
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void givenConnection_thenFetchDbData() throws SQLException {
|
||||
List<Employee> employees = HikariCPDemo.fetchData();
|
||||
assertEquals(4, employees.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenConnection_thenFetchDbData() {
|
||||
List<Employee> employees = HikariCPDemo.fetchData();
|
||||
assertEquals(4, employees.size());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user