Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2017
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.46 KB | None | 0 0
  1. class LightsTest extends Thread
  2. {
  3. {
  4. boolean lightsLoop = true;
  5.  
  6. int bedState = 3;
  7. int oldState = 3;
  8.  
  9.  
  10.  
  11. System.out.println("Loading driver...");
  12.  
  13. try
  14. {
  15. Class.forName("com.mysql.jdbc.Driver");
  16. System.out.println("Driver loaded!");
  17. }
  18. catch (ClassNotFoundException e)
  19. {
  20. throw new IllegalStateException("Cannot find the driver in the classpath!", e);
  21. }
  22.  
  23.  
  24. System.out.println("Connecting to database...");
  25. Connection db = null;
  26. try
  27. {
  28. db = DriverManager.getConnection("jdbc:mysql://93.107.48.54:3306/test","root","Project@2017");
  29. }
  30. catch (SQLException e2)
  31. {
  32. // TODO Auto-generated catch block
  33. e2.printStackTrace();
  34. }
  35.  
  36. while (lightsLoop == true)
  37. {
  38. String query = " SELECT * FROM bedroom1";
  39.  
  40. Statement st = null;
  41. try
  42. {
  43. st = db.createStatement();
  44. }
  45. catch (SQLException e2)
  46. {
  47. // TODO Auto-generated catch block
  48. e2.printStackTrace();
  49. }
  50.  
  51. ResultSet rs = null;
  52. try
  53. {
  54. rs = st.executeQuery(query);
  55. }
  56. catch (SQLException e2)
  57. {
  58. // TODO Auto-generated catch block
  59. e2.printStackTrace();
  60. }
  61.  
  62. try
  63. {
  64. rs.next();
  65. }
  66. catch (SQLException e3)
  67. {
  68. // TODO Auto-generated catch block
  69. e3.printStackTrace();
  70. }
  71.  
  72. try
  73. {
  74. bedState = rs.getInt("state");
  75. }
  76. catch (SQLException e2)
  77. {
  78. // TODO Auto-generated catch block
  79. e2.printStackTrace();
  80. }
  81. System.out.println(bedState);
  82.  
  83. try
  84. {
  85. st.close();
  86. }
  87. catch (SQLException e2)
  88. {
  89. // TODO Auto-generated catch block
  90. e2.printStackTrace();
  91. }
  92.  
  93. System.out.println(oldState);
  94.  
  95. if (bedState != oldState)
  96. {
  97. System.out.println("Client Starting..");
  98. InetAddress inet = null;
  99. try
  100. {
  101. inet = InetAddress.getByName("93.107.48.54");
  102.  
  103. }
  104. catch (UnknownHostException e3)
  105. {
  106. // TODO Auto-generated catch block
  107. e3.printStackTrace();
  108. }
  109.  
  110. Socket socket = null;
  111.  
  112. try
  113. {
  114. socket = new Socket(inet, 2001);
  115. }
  116. catch (IOException e2)
  117. {
  118. // TODO Auto-generated catch block
  119. e2.printStackTrace();
  120. }
  121. OutputStream outStream = null;
  122.  
  123. try
  124. {
  125. outStream = socket.getOutputStream();
  126. }
  127. catch (IOException e1)
  128. {
  129. // TODO Auto-generated catch block
  130. e1.printStackTrace();
  131. }
  132. PrintWriter p = new PrintWriter(outStream);
  133.  
  134. p.println("light");
  135. p.flush();
  136.  
  137.  
  138. if(bedState == 0)
  139. {
  140. p.println("on");
  141. p.flush();
  142. System.out.println("On");
  143. }
  144. else if (bedState == 1)
  145. {
  146. p.println("off");
  147. p.flush();
  148. System.out.println("Off");
  149.  
  150. }
  151.  
  152. try
  153. {
  154. Thread.sleep(500);
  155. }
  156. catch (InterruptedException e)
  157. {
  158. // TODO Auto-generated catch block
  159. e.printStackTrace();
  160. }
  161.  
  162. oldState = bedState;
  163. System.out.println(oldState);
  164.  
  165.  
  166. }
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement