Advertisement
Guest User

Untitled

a guest
Apr 21st, 2017
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.29 KB | None | 0 0
  1. package project3v2;
  2.  
  3. import java.io.BufferedWriter;
  4. import java.io.FileWriter;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.io.OutputStream;
  8. import java.io.PrintWriter;
  9. import java.net.InetAddress;
  10. import java.net.Socket;
  11. import java.net.UnknownHostException;
  12. import java.sql.Connection;
  13. import java.sql.DriverManager;
  14. import java.sql.ResultSet;
  15. import java.sql.SQLException;
  16. import java.sql.Statement;
  17. import java.text.DateFormat;
  18. import java.text.SimpleDateFormat;
  19. import java.util.Date;
  20. import java.util.Scanner;
  21.  
  22. class TempsTest extends Thread
  23. {
  24. @SuppressWarnings("resource")
  25. public void run()
  26. {
  27. boolean start = true;
  28.  
  29. while (start == true)
  30. {
  31. System.out.println("Client Starting..");
  32. InetAddress inet = null;
  33. try {
  34. inet = InetAddress.getByName("93.107.48.54");
  35. } catch (UnknownHostException e3) {
  36. // TODO Auto-generated catch block
  37. e3.printStackTrace();
  38. }
  39. Socket socket = null;
  40. try {
  41. socket = new Socket(inet, 2001);
  42. } catch (IOException e2) {
  43. // TODO Auto-generated catch block
  44. e2.printStackTrace();
  45. }
  46.  
  47. OutputStream outStream = null;
  48. try {
  49. outStream = socket.getOutputStream();
  50. } catch (IOException e1) {
  51. // TODO Auto-generated catch block
  52. e1.printStackTrace();
  53. }
  54. PrintWriter p = new PrintWriter(outStream);
  55.  
  56. p.println("temp");
  57. p.flush();
  58.  
  59.  
  60. InputStream inStream = null;
  61. try {
  62. inStream = socket.getInputStream();
  63. } catch (IOException e1) {
  64. // TODO Auto-generated catch block
  65. e1.printStackTrace();
  66. }
  67. Scanner input = new Scanner(inStream);
  68. String str = input.nextLine();
  69. System.out.println("Current temperature: " + str);
  70. DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
  71. Date date = new Date();
  72. String dateString = dateFormat.format(date);
  73. DateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
  74. Date time = new Date();
  75. String timeString = timeFormat.format(time);
  76. System.out.println("Current date (y-m-d): " + dateString);
  77. System.out.println("Current time (h:m:s): " + timeString);
  78.  
  79. System.out.println("Loading driver...");
  80. try {
  81. Class.forName("com.mysql.jdbc.Driver");
  82. System.out.println("Driver loaded!");
  83. } catch (ClassNotFoundException e) {
  84. throw new IllegalStateException("Cannot find the driver in the classpath!", e);
  85. }
  86.  
  87. try {
  88. System.out.println("Connecting to database...");
  89. Connection db = DriverManager.getConnection("jdbc:mysql://93.107.48.54:3306/comfi","root","Project@2017");
  90. System.out.println("Creating statement...");
  91. Statement st = db.createStatement();
  92. String sql = "INSERT INTO temperature (date, time, temp) values ('" + dateString + "', '" + timeString + "', '" + str + "');";
  93. System.out.println("Insterting data to database...");
  94. st.executeUpdate(sql);
  95. System.out.println("Data successfully inserted into database!");
  96. }catch (Exception exc) {
  97. exc.printStackTrace();
  98. }
  99. try {
  100. Thread.sleep(15000);
  101. } catch (InterruptedException e) {
  102. // TODO Auto-generated catch block
  103. e.printStackTrace();
  104. }
  105. }
  106. }
  107. }
  108.  
  109. class LightsTest extends Thread
  110. {
  111. {
  112. int bedState = 3;
  113. System.out.println("Client Starting..");
  114. InetAddress inet = null;
  115. try
  116. {
  117. inet = InetAddress.getByName("93.107.48.54");
  118. }
  119. catch (UnknownHostException e3)
  120. {
  121. // TODO Auto-generated catch block
  122. e3.printStackTrace();
  123. }
  124.  
  125. Socket socket = null;
  126.  
  127. try
  128. {
  129. socket = new Socket(inet, 2001);
  130. }
  131. catch (IOException e2)
  132. {
  133. // TODO Auto-generated catch block
  134. e2.printStackTrace();
  135. }
  136.  
  137. System.out.println("Loading driver...");
  138.  
  139. try
  140. {
  141. Class.forName("com.mysql.jdbc.Driver");
  142. System.out.println("Driver loaded!");
  143. }
  144. catch (ClassNotFoundException e)
  145. {
  146. throw new IllegalStateException("Cannot find the driver in the classpath!", e);
  147. }
  148.  
  149.  
  150. System.out.println("Connecting to database...");
  151. Connection db = null;
  152. try
  153. {
  154. db = DriverManager.getConnection("jdbc:mysql://93.107.48.54:3306/test","root","Project@2017");
  155. }
  156. catch (SQLException e2)
  157. {
  158. // TODO Auto-generated catch block
  159. e2.printStackTrace();
  160. }
  161.  
  162. String query = " SELECT * FROM bedroom1";
  163.  
  164. Statement st = null;
  165. try
  166. {
  167. st = db.createStatement();
  168. }
  169. catch (SQLException e2)
  170. {
  171. // TODO Auto-generated catch block
  172. e2.printStackTrace();
  173. }
  174.  
  175. ResultSet rs = null;
  176. try
  177. {
  178. rs = st.executeQuery(query);
  179. }
  180. catch (SQLException e2)
  181. {
  182. // TODO Auto-generated catch block
  183. e2.printStackTrace();
  184. }
  185.  
  186. try
  187. {
  188. bedState = rs.getInt("state");
  189. }
  190. catch (SQLException e2)
  191. {
  192. // TODO Auto-generated catch block
  193. e2.printStackTrace();
  194. }
  195.  
  196. try
  197. {
  198. st.close();
  199. }
  200. catch (SQLException e2)
  201. {
  202. // TODO Auto-generated catch block
  203. e2.printStackTrace();
  204. }
  205.  
  206.  
  207.  
  208.  
  209. OutputStream outStream = null;
  210.  
  211. try
  212. {
  213. outStream = socket.getOutputStream();
  214. }
  215. catch (IOException e1)
  216. {
  217. // TODO Auto-generated catch block
  218. e1.printStackTrace();
  219. }
  220. PrintWriter p = new PrintWriter(outStream);
  221.  
  222. p.println("light");
  223. p.flush();
  224.  
  225.  
  226. if(bedState == 0)
  227. {
  228. p.println("on");
  229. p.flush();
  230. }
  231. else if (bedState == 1)
  232. {
  233. p.println("off");
  234. p.flush();
  235. }
  236.  
  237. try
  238. {
  239. Thread.sleep(200);
  240. }
  241. catch (InterruptedException e)
  242. {
  243. // TODO Auto-generated catch block
  244. e.printStackTrace();
  245. }
  246. }
  247. }
  248.  
  249. public class ServerSockets8
  250. {
  251. public static void main(String[] args) throws Exception
  252. {
  253. //Thread newTemp = new TempsTest();
  254. //newTemp.start();
  255.  
  256. Thread newLight = new LightsTest();
  257. newLight.start();
  258. }
  259. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement