Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class DataSource {
- private static DataSource datasource;
- private BasicDataSource ds;
- private DataSource() throws IOException, SQLException, PropertyVetoException {
- ds = new BasicDataSource();
- ds.setDriverClassName("org.postgresql.Driver");
- ds.setUsername("postgres");
- ds.setPassword("postgres");
- ds.setUrl("jdbc:postgresql://localhost:5432/ehealth");
- // the settings below are optional -- dbcp can work with defaults
- // ds.setMinIdle(5);
- // ds.setMaxIdle(20);
- // ds.setMaxOpenPreparedStatements(180);
- }
- public static DataSource getInstance() throws IOException, SQLException, PropertyVetoException {
- if (datasource == null) {
- datasource = new DataSource();
- return datasource;
- } else {
- return datasource;
- }
- }
- public Connection getConnection() throws SQLException {
- return this.ds.getConnection();
- }
- }
- public static Map<String, List<PreparedStatement>> test(int numberOfRequest) throws SQLException{
- try(Connection con = getHealthConnection(); Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
- ResultSet.CONCUR_READ_ONLY,
- ResultSet.HOLD_CURSORS_OVER_COMMIT);){
- Map<String, List<PreparedStatement>> myMap = new HashMap<String, List<PreparedStatement>>();
- List<PreparedStatement> lstPreparedStmt = new ArrayList<PreparedStatement>();
- String request = "INSERT INTO PATIENT VALUES (?, ?, ?, ?, 'M', 'OTHER')";
- myMap.put(request, null);
- int counter = 0;
- String rqstSelect = "SELECT * FROM patient ORDER BY pat_id"; //initialize SELECT request
- ResultSet resRequest = stmt.executeQuery(rqstSelect); //execute SELECT
- String newId = createNewIdByIncrement(resRequest); //initialize first new id
- while(counter < numberOfRequest){
- String newName = generateRandomString(); //generate random new name
- String newLast = generateRandomString(); //generate random new last name
- String newLast2 = generateRandomString(); //generate random new last name 2
- PreparedStatement prep = con.prepareStatement(request, ResultSet.TYPE_SCROLL_INSENSITIVE,
- ResultSet.CONCUR_READ_ONLY,
- ResultSet.HOLD_CURSORS_OVER_COMMIT);
- prep.setString(1, newId);
- prep.setString(2, newName);
- prep.setString(3, newLast);
- prep.setString(4, newLast2);
- lstPreparedStmt.add(prep);
- counter = counter + 1; //increment counter
- int newIdAsInt = Integer.parseInt(newId) + 1; //increment id
- newId = concatenationZeroIntAsString(newIdAsInt, 8);
- }
- myMap.put(request, lstPreparedStmt);
- return myMap;
- }
- }
- private void insertPreparedStatementRequest(int numberOfRequest) throws SQLException, IOException, PropertyVetoException{
- try(Connection con = DataSource.getInstance().getConnection(); Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
- ResultSet.CONCUR_READ_ONLY,
- ResultSet.HOLD_CURSORS_OVER_COMMIT);){
- // check number of row before insert
- String rqstCountRowBefore = "SELECT * FROM patient ORDER BY pat_id";
- ResultSet resCheckBefore = stmt.executeQuery(rqstCountRowBefore);
- int rowBefore = TestUtils.getRowCount(resCheckBefore);
- Assert.assertNotNull("Request's result is null", resCheckBefore);
- // insert X row
- Map<String, List<PreparedStatement>> allRequest = TestUtils.test(numberOfRequest);
- for(Map.Entry<String, List<PreparedStatement>> e : allRequest.entrySet()){
- List<PreparedStatement> lstStatement = e.getValue();
- Iterator<PreparedStatement> valIterator = lstStatement.iterator();
- while(valIterator.hasNext()){
- valIterator.next().execute();
- }
- }
- // check number of row after insert
- String rqstCountrowAfter = "SELECT * FROM patient ORDER BY pat_id";
- ResultSet resCheckAfter = stmt.executeQuery(rqstCountrowAfter);
- int rowAfter = TestUtils.getRowCount(resCheckAfter);
- Assert.assertNotNull("Request's result is null", resCheckBefore);
- Assert.assertEquals("Insert result's request unsuccesfull", rowBefore + numberOfRequest, rowAfter);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement