Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class LightsTest extends Thread
- {
- {
- boolean lightsLoop = true;
- int bedState = 3;
- int oldState = 3;
- System.out.println("Loading driver...");
- try
- {
- Class.forName("com.mysql.jdbc.Driver");
- System.out.println("Driver loaded!");
- }
- catch (ClassNotFoundException e)
- {
- throw new IllegalStateException("Cannot find the driver in the classpath!", e);
- }
- System.out.println("Connecting to database...");
- Connection db = null;
- try
- {
- db = DriverManager.getConnection("jdbc:mysql://93.107.48.54:3306/test","root","Project@2017");
- }
- catch (SQLException e2)
- {
- // TODO Auto-generated catch block
- e2.printStackTrace();
- }
- while (lightsLoop == true)
- {
- String query = " SELECT * FROM bedroom1";
- Statement st = null;
- try
- {
- st = db.createStatement();
- }
- catch (SQLException e2)
- {
- // TODO Auto-generated catch block
- e2.printStackTrace();
- }
- ResultSet rs = null;
- try
- {
- rs = st.executeQuery(query);
- }
- catch (SQLException e2)
- {
- // TODO Auto-generated catch block
- e2.printStackTrace();
- }
- try
- {
- rs.next();
- }
- catch (SQLException e3)
- {
- // TODO Auto-generated catch block
- e3.printStackTrace();
- }
- try
- {
- bedState = rs.getInt("state");
- }
- catch (SQLException e2)
- {
- // TODO Auto-generated catch block
- e2.printStackTrace();
- }
- System.out.println(bedState);
- try
- {
- st.close();
- }
- catch (SQLException e2)
- {
- // TODO Auto-generated catch block
- e2.printStackTrace();
- }
- System.out.println(oldState);
- if (bedState != oldState)
- {
- System.out.println("Client Starting..");
- InetAddress inet = null;
- try
- {
- inet = InetAddress.getByName("93.107.48.54");
- }
- catch (UnknownHostException e3)
- {
- // TODO Auto-generated catch block
- e3.printStackTrace();
- }
- Socket socket = null;
- try
- {
- socket = new Socket(inet, 2001);
- }
- catch (IOException e2)
- {
- // TODO Auto-generated catch block
- e2.printStackTrace();
- }
- OutputStream outStream = null;
- try
- {
- outStream = socket.getOutputStream();
- }
- catch (IOException e1)
- {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
- PrintWriter p = new PrintWriter(outStream);
- p.println("light");
- p.flush();
- if(bedState == 0)
- {
- p.println("on");
- p.flush();
- System.out.println("On");
- }
- else if (bedState == 1)
- {
- p.println("off");
- p.flush();
- System.out.println("Off");
- }
- try
- {
- Thread.sleep(500);
- }
- catch (InterruptedException e)
- {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- oldState = bedState;
- System.out.println(oldState);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement