Advertisement
Guest User

Untitled

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