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.Statement;
- import java.text.DateFormat;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import java.util.Scanner;
- class Temps 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 Lights extends Thread
- {
- public void run()
- {
- System.out.println("Client Starting..");
- InetAddress inet = null;
- try {
- inet = InetAddress.getByName("192.168.0.5");
- } 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();
- int x = 1;
- if(x%2 == 0)
- {
- p.println("on");
- p.flush();
- x+=1;
- }
- else if (x%2 == 1)
- {
- p.println("off");
- p.flush();
- x+=1;
- }
- try {
- Thread.sleep(5000);
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- public class ServerSockets5
- {
- public static void main(String[] args) throws Exception
- {
- Thread newTemp = new Temps();
- newTemp.start();
- //Thread newLight = new Lights();
- //newLight.start();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement