Advertisement
Guest User

Untitled

a guest
Dec 5th, 2016
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.58 KB | None | 0 0
  1. public class DataSource {
  2.  
  3. private static DataSource datasource;
  4. private BasicDataSource ds;
  5.  
  6. private DataSource() throws IOException, SQLException, PropertyVetoException {
  7. ds = new BasicDataSource();
  8. ds.setDriverClassName("org.postgresql.Driver");
  9. ds.setUsername("postgres");
  10. ds.setPassword("postgres");
  11. ds.setUrl("jdbc:postgresql://localhost:5432/ehealth");
  12.  
  13. // the settings below are optional -- dbcp can work with defaults
  14. // ds.setMinIdle(5);
  15. // ds.setMaxIdle(20);
  16. // ds.setMaxOpenPreparedStatements(180);
  17.  
  18. }
  19.  
  20. public static DataSource getInstance() throws IOException, SQLException, PropertyVetoException {
  21. if (datasource == null) {
  22. datasource = new DataSource();
  23. return datasource;
  24. } else {
  25. return datasource;
  26. }
  27. }
  28.  
  29. public Connection getConnection() throws SQLException {
  30. return this.ds.getConnection();
  31. }
  32.  
  33. }
  34.  
  35. public static Map<String, List<PreparedStatement>> test(int numberOfRequest) throws SQLException{
  36. try(Connection con = getHealthConnection(); Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
  37. ResultSet.CONCUR_READ_ONLY,
  38. ResultSet.HOLD_CURSORS_OVER_COMMIT);){
  39. Map<String, List<PreparedStatement>> myMap = new HashMap<String, List<PreparedStatement>>();
  40. List<PreparedStatement> lstPreparedStmt = new ArrayList<PreparedStatement>();
  41. String request = "INSERT INTO PATIENT VALUES (?, ?, ?, ?, 'M', 'OTHER')";
  42. myMap.put(request, null);
  43. int counter = 0;
  44. String rqstSelect = "SELECT * FROM patient ORDER BY pat_id"; //initialize SELECT request
  45. ResultSet resRequest = stmt.executeQuery(rqstSelect); //execute SELECT
  46. String newId = createNewIdByIncrement(resRequest); //initialize first new id
  47. while(counter < numberOfRequest){
  48. String newName = generateRandomString(); //generate random new name
  49. String newLast = generateRandomString(); //generate random new last name
  50. String newLast2 = generateRandomString(); //generate random new last name 2
  51. PreparedStatement prep = con.prepareStatement(request, ResultSet.TYPE_SCROLL_INSENSITIVE,
  52. ResultSet.CONCUR_READ_ONLY,
  53. ResultSet.HOLD_CURSORS_OVER_COMMIT);
  54. prep.setString(1, newId);
  55. prep.setString(2, newName);
  56. prep.setString(3, newLast);
  57. prep.setString(4, newLast2);
  58. lstPreparedStmt.add(prep);
  59. counter = counter + 1; //increment counter
  60. int newIdAsInt = Integer.parseInt(newId) + 1; //increment id
  61. newId = concatenationZeroIntAsString(newIdAsInt, 8);
  62. }
  63. myMap.put(request, lstPreparedStmt);
  64. return myMap;
  65. }
  66. }
  67.  
  68. private void insertPreparedStatementRequest(int numberOfRequest) throws SQLException, IOException, PropertyVetoException{
  69. try(Connection con = DataSource.getInstance().getConnection(); Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
  70. ResultSet.CONCUR_READ_ONLY,
  71. ResultSet.HOLD_CURSORS_OVER_COMMIT);){
  72. // check number of row before insert
  73. String rqstCountRowBefore = "SELECT * FROM patient ORDER BY pat_id";
  74. ResultSet resCheckBefore = stmt.executeQuery(rqstCountRowBefore);
  75. int rowBefore = TestUtils.getRowCount(resCheckBefore);
  76. Assert.assertNotNull("Request's result is null", resCheckBefore);
  77. // insert X row
  78. Map<String, List<PreparedStatement>> allRequest = TestUtils.test(numberOfRequest);
  79. for(Map.Entry<String, List<PreparedStatement>> e : allRequest.entrySet()){
  80. List<PreparedStatement> lstStatement = e.getValue();
  81. Iterator<PreparedStatement> valIterator = lstStatement.iterator();
  82. while(valIterator.hasNext()){
  83. valIterator.next().execute();
  84. }
  85. }
  86. // check number of row after insert
  87. String rqstCountrowAfter = "SELECT * FROM patient ORDER BY pat_id";
  88. ResultSet resCheckAfter = stmt.executeQuery(rqstCountrowAfter);
  89. int rowAfter = TestUtils.getRowCount(resCheckAfter);
  90. Assert.assertNotNull("Request's result is null", resCheckBefore);
  91. Assert.assertEquals("Insert result's request unsuccesfull", rowBefore + numberOfRequest, rowAfter);
  92. }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement