Advertisement
Khadija_Assem

Untitled

Dec 6th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.78 KB | None | 0 0
  1. package eg.edu.alexu.csd.oop.JDBC.Statement;
  2.  
  3. import eg.edu.alexu.csd.oop.DBMSAdaptar;
  4. import eg.edu.alexu.csd.oop.DBMSAdapterPool;
  5. import eg.edu.alexu.csd.oop.JDBC.Connection.DBConnectionPool;
  6.  
  7. import java.sql.BatchUpdateException;
  8. import java.sql.Connection;
  9. import java.sql.ResultSet;
  10. import java.sql.SQLException;
  11. import java.util.LinkedList;
  12. import java.util.Queue;
  13. import java.util.concurrent.*;
  14.  
  15. public class DBStatementAdapter extends DBStatement{
  16. private Queue<String> SQLQueries = new LinkedList<>();
  17. private int timeOut = 0 ;
  18. private DBMSAdaptar adapter;
  19. private ResultSet resultSet;
  20. private String path;
  21. private int updateCount;
  22.  
  23. public DBStatementAdapter(String path) {
  24. this.adapter= DBMSAdapterPool.getinstance();
  25. this.path = path;
  26. }
  27.  
  28.  
  29. public void addBatch(String sql) throws SQLException {
  30. this.SQLQueries.add(sql);
  31. }
  32.  
  33. public void clearBatch() throws SQLException {
  34. this.SQLQueries.clear();
  35. }
  36.  
  37. public void close() throws SQLException {
  38. clearBatch();
  39. adapter=null;
  40. this.SQLQueries = null;
  41. }
  42.  
  43. public int[] executeBatch() throws BatchUpdateException {
  44.  
  45. int[] arr = new int[SQLQueries.size()];
  46.  
  47. for(int i = 0 ; i < arr.length ; i++) {
  48.  
  49. String sql = SQLQueries.poll();
  50. Check check = new Check(sql);
  51. String checker = check.Checker();
  52. if (checker.equalsIgnoreCase("C")) {
  53.  
  54. try {
  55. execute(sql);
  56. arr[i] = 0 ;
  57. } catch (SQLException e) {
  58. throw new BatchUpdateException();
  59. }
  60. }
  61. else if (checker.equalsIgnoreCase("U")){
  62.  
  63. try {
  64. int z = executeUpdate(sql) ;
  65. arr[i] =z ;
  66. } catch (SQLException e) {
  67. throw new BatchUpdateException() ;
  68. }
  69. }
  70. else{
  71. throw new BatchUpdateException() ;
  72. }
  73. }
  74. return arr ;
  75. }
  76.  
  77.  
  78. public Connection getConnection() throws SQLException {
  79. return DBConnectionPool.getConnection(path);
  80. }
  81.  
  82. public int getQueryTimeout() throws SQLException {
  83. return this.timeOut ;
  84. }
  85.  
  86. public void setQueryTimeout(int seconds) throws SQLException {
  87. this.timeOut = seconds ;
  88. }
  89. public boolean execute(String sql) throws SQLException {// excute and if done return true
  90. ExecutorService excutor = Executors.newSingleThreadExecutor() ;
  91. boolean returned ;
  92. Future <Boolean> future = excutor.submit(new Task(sql));
  93. if(getQueryTimeout() != 0 ) {
  94. try {
  95. returned = future.get(getQueryTimeout(), TimeUnit.SECONDS);
  96. return returned ;
  97.  
  98. } catch (InterruptedException e) {
  99. e.printStackTrace();
  100. throw new SQLException();
  101.  
  102. } catch (ExecutionException e) {
  103. e.printStackTrace();
  104. throw new SQLException();
  105.  
  106. } catch (TimeoutException e) {
  107. e.printStackTrace();
  108. throw new SQLException();
  109.  
  110. }
  111. }else {
  112. try {
  113. returned = future.get();
  114. return returned ;
  115.  
  116. } catch (InterruptedException e) {
  117. e.printStackTrace();
  118. throw new SQLException();
  119.  
  120. } catch (ExecutionException e) {
  121. e.printStackTrace();
  122. throw new SQLException();
  123.  
  124. }
  125. }
  126. }
  127.  
  128.  
  129. public ResultSet executeQuery(String sql) throws SQLException {
  130. ExecutorService excutor = Executors.newSingleThreadExecutor() ;
  131. Future <Boolean> future = excutor.submit(new Task(sql));
  132. if(getQueryTimeout() != 0 ) {
  133. try {
  134. future.get(getQueryTimeout(), TimeUnit.SECONDS);
  135. return resultSet ;
  136.  
  137. } catch (InterruptedException e) {
  138. e.printStackTrace();
  139. throw new SQLException();
  140.  
  141. } catch (ExecutionException e) {
  142. e.printStackTrace();
  143. throw new SQLException();
  144.  
  145. } catch (TimeoutException e) {
  146. e.printStackTrace();
  147. throw new SQLException();
  148.  
  149. }
  150. }else {
  151. try {
  152. future.get();
  153. return resultSet ;
  154.  
  155. } catch (InterruptedException e) {
  156. e.printStackTrace();
  157. throw new SQLException();
  158.  
  159. } catch (ExecutionException e) {
  160. e.printStackTrace();
  161. throw new SQLException();
  162.  
  163. }
  164. }
  165. }
  166.  
  167.  
  168. public int executeUpdate(String sql) throws SQLException {
  169. ExecutorService excutor = Executors.newSingleThreadExecutor() ;
  170. Future <Boolean> future = excutor.submit(new Task(sql));
  171. if(getQueryTimeout() != 0 ) {
  172. try {
  173. future.get(getQueryTimeout(), TimeUnit.SECONDS);
  174. return updateCount ;
  175.  
  176. } catch (InterruptedException e) {
  177. e.printStackTrace();
  178. throw new SQLException();
  179.  
  180. } catch (ExecutionException e) {
  181. e.printStackTrace();
  182. throw new SQLException();
  183.  
  184. } catch (TimeoutException e) {
  185. e.printStackTrace();
  186. throw new SQLException();
  187.  
  188. }
  189. }else {
  190. try {
  191. future.get();
  192. return updateCount ;
  193.  
  194. } catch (InterruptedException e) {
  195. e.printStackTrace();
  196. throw new SQLException();
  197.  
  198. } catch (ExecutionException e) {
  199. e.printStackTrace();
  200. throw new SQLException();
  201.  
  202. }
  203. }
  204. }
  205.  
  206. class Task implements Callable<Boolean>{
  207. String query;
  208. private Object SQLException;
  209.  
  210. public Task (String query){
  211. this.query=query;
  212. }
  213. @Override
  214. public Boolean call() throws Exception {
  215. Check check = new Check(query);
  216. String checker = check.Checker();
  217. if(checker.equals("C")){
  218. return adapter.createDatabase(query);
  219. }
  220. else if (checker.equalsIgnoreCase("U")) {
  221. updateCount=adapter.update(query) ;
  222. return true ;
  223. }
  224. else if (checker.equalsIgnoreCase("S")){
  225. resultSet =executeQuery(query) ;
  226. return true ;
  227. }
  228. else{
  229. throw new SQLException() ;
  230. }
  231. }
  232. }
  233.  
  234. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement