Advertisement
Guest User

Untitled

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