Advertisement
Guest User

Untitled

a guest
Jun 15th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.00 KB | None | 0 0
  1. package de.stuckdexter.sophiadb;
  2.  
  3. import com.sun.deploy.util.StringUtils;
  4. import de.stuckdexter.sophiadb.exceptions.*;
  5.  
  6. import java.io.*;
  7. import java.net.InetAddress;
  8. import java.net.Socket;
  9.  
  10. public class SophiaDbConnection{
  11.  
  12. private String username;
  13. private String password;
  14. private String address;
  15.  
  16. public SophiaDbConnection(String username, String password, String address){
  17. this.username = username;
  18. this.password = password;
  19. this.address = address;
  20. }
  21.  
  22. private String request(String message){
  23. Socket sock = null;
  24. try {
  25. sock = new Socket(InetAddress.getByName(address), 3000);
  26. } catch (IOException e) {
  27. e.printStackTrace();
  28. }
  29. try {
  30. PrintWriter printWriter = null;
  31. if (sock != null) {
  32. printWriter = new PrintWriter(new OutputStreamWriter(sock.getOutputStream()));
  33. printWriter.print(message);
  34. printWriter.flush();
  35. }
  36. } catch (IOException e) {
  37. e.printStackTrace();
  38. }
  39. String nachricht = "";
  40. BufferedReader bufferedReader =
  41. null;
  42. try {
  43. bufferedReader = new BufferedReader(
  44. new InputStreamReader(
  45. sock.getInputStream()));
  46. char[] buffer = new char[1024];
  47. int anzahlZeichen = bufferedReader.read(buffer, 0, 1024);
  48. nachricht = new String(buffer, 0, anzahlZeichen);
  49. } catch (IOException e) {
  50. e.printStackTrace();
  51. }
  52. return nachricht;
  53. }
  54.  
  55. public String[] get(String table, String row) throws NotExistException, AlreadyExistException, SynatxNotValidException, CmdNotExistException, TableNotExistException, InvalidLoginException, NoPermissionException{
  56. String result = request("{" + username + "," + password + "}" + table + ";get;" + row);
  57.  
  58. switch (result){
  59. case "error1":
  60. throw new NotExistException();
  61. case "error2":
  62. throw new AlreadyExistException();
  63. case "error3":
  64. throw new SynatxNotValidException();
  65. case "error4":
  66. throw new CmdNotExistException();
  67. case "error5":
  68. throw new TableNotExistException();
  69. case "error6":
  70. throw new InvalidLoginException();
  71. case "error7":
  72. throw new NoPermissionException();
  73. default:
  74. result = result.replace("'", "");
  75. result = result.replace("[", "");
  76. result = result.replace("]", "");
  77.  
  78. return result.split(",");
  79. }
  80. }
  81.  
  82. public String[] getAll(String table, String row) throws NotExistException, AlreadyExistException, SynatxNotValidException, CmdNotExistException, TableNotExistException, InvalidLoginException, NoPermissionException{
  83. String result = request("{" + username + "," + password + "}" + table + ";getall");
  84.  
  85. switch (result){
  86. case "error1":
  87. throw new NotExistException();
  88. case "error2":
  89. throw new AlreadyExistException();
  90. case "error3":
  91. throw new SynatxNotValidException();
  92. case "error4":
  93. throw new CmdNotExistException();
  94. case "error5":
  95. throw new TableNotExistException();
  96. case "error6":
  97. throw new InvalidLoginException();
  98. case "error7":
  99. throw new NoPermissionException();
  100. default:
  101. result = result.replace("'", "");
  102. result = result.replace("[", "");
  103. result = result.replace("]", "");
  104.  
  105. return result.split(",");
  106. }
  107. }
  108.  
  109. public boolean set(String table, String row, String[] data) throws NotExistException, AlreadyExistException, SynatxNotValidException, CmdNotExistException, TableNotExistException, InvalidLoginException, NoPermissionException{
  110. String dat = "[" + String.join(",", data) + "]";
  111. String result = request("{" + username + "," + password + "}" + table + ";set;" + row + ";" + dat);
  112.  
  113. switch (result){
  114. case "error1":
  115. throw new NotExistException();
  116. case "error2":
  117. throw new AlreadyExistException();
  118. case "error3":
  119. throw new SynatxNotValidException();
  120. case "error4":
  121. throw new CmdNotExistException();
  122. case "error5":
  123. throw new TableNotExistException();
  124. case "error6":
  125. throw new InvalidLoginException();
  126. case "error7":
  127. throw new NoPermissionException();
  128. case "OK":
  129. return true;
  130. }
  131. return false;
  132. }
  133.  
  134. public boolean add(String table, String row, String[] data) throws NotExistException, AlreadyExistException, SynatxNotValidException, CmdNotExistException, TableNotExistException, InvalidLoginException, NoPermissionException{
  135. String dat = "[" + String.join(",", data) + "]";
  136. String result = request("{" + username + "," + password + "}" + table + ";add;" + row + ";" + dat);
  137.  
  138. switch (result){
  139. case "error1":
  140. throw new NotExistException();
  141. case "error2":
  142. throw new AlreadyExistException();
  143. case "error3":
  144. throw new SynatxNotValidException();
  145. case "error4":
  146. throw new CmdNotExistException();
  147. case "error5":
  148. throw new TableNotExistException();
  149. case "error6":
  150. throw new InvalidLoginException();
  151. case "error7":
  152. throw new NoPermissionException();
  153. case "OK":
  154. return true;
  155. }
  156. return false;
  157. }
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement