Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package project3v2;
- import java.io.BufferedWriter;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.io.PrintWriter;
- import java.net.InetAddress;
- import java.net.Socket;
- import java.net.UnknownHostException;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.text.DateFormat;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import java.util.Scanner;
- class TempsTest extends Thread
- {
- @SuppressWarnings("resource")
- public void run()
- {
- boolean start = true;
- while (start == true)
- {
- 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("temp");
- p.flush();
- InputStream inStream = null;
- try {
- inStream = socket.getInputStream();
- } catch (IOException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
- Scanner input = new Scanner(inStream);
- String str = input.nextLine();
- System.out.println("Current temperature: " + str);
- DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
- Date date = new Date();
- String dateString = dateFormat.format(date);
- DateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
- Date time = new Date();
- String timeString = timeFormat.format(time);
- System.out.println("Current date (y-m-d): " + dateString);
- System.out.println("Current time (h:m:s): " + timeString);
- 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);
- }
- try {
- System.out.println("Connecting to database...");
- Connection db = DriverManager.getConnection("jdbc:mysql://93.107.48.54:3306/comfi","root","Project@2017");
- System.out.println("Creating statement...");
- Statement st = db.createStatement();
- String sql = "INSERT INTO temperature (date, time, temp) values ('" + dateString + "', '" + timeString + "', '" + str + "');";
- System.out.println("Insterting data to database...");
- st.executeUpdate(sql);
- System.out.println("Data successfully inserted into database!");
- }catch (Exception exc) {
- exc.printStackTrace();
- }
- try {
- Thread.sleep(15000);
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- }
- class LightsTest extends Thread
- {
- {
- int bedState = 3;
- 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();
- }
- 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();
- }
- 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
- {
- bedState = rs.getInt("state");
- }
- catch (SQLException e2)
- {
- // TODO Auto-generated catch block
- e2.printStackTrace();
- }
- try
- {
- st.close();
- }
- catch (SQLException 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();
- }
- else if (bedState == 1)
- {
- p.println("off");
- p.flush();
- }
- try
- {
- Thread.sleep(200);
- }
- catch (InterruptedException e)
- {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- public class ServerSockets8
- {
- public static void main(String[] args) throws Exception
- {
- //Thread newTemp = new TempsTest();
- //newTemp.start();
- Thread newLight = new LightsTest();
- newLight.start();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement