Commit bdc81554 authored by Jonathan Poalses's avatar Jonathan Poalses

Added database support

parent e6549c17
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="DataSourceManagerImpl" format="xml" multifile-model="true">
<data-source source="LOCAL" name="iotserver" uuid="2e5e548e-7ddb-42fe-8e7e-fc032c2917a5">
<driver-ref>sqlite.xerial</driver-ref>
<synchronize>true</synchronize>
<jdbc-driver>org.sqlite.JDBC</jdbc-driver>
<jdbc-url>jdbc:sqlite:$PROJECT_DIR$/../../databases/iot/iotserver</jdbc-url>
<jdbc-additional-properties>
<property name="EXTERNAL_DATA_PATH" value="$PROJECT_DIR$/test.xml" />
</jdbc-additional-properties>
<working-dir>$ProjectFileDir$</working-dir>
</data-source>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
......@@ -10,6 +10,7 @@ repositories {
}
dependencies {
implementation 'org.xerial:sqlite-jdbc:3.40.0.0'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
}
......
......@@ -2,6 +2,11 @@ package jonathan.poalses;
import java.io.*;
import java.net.Socket;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.time.Instant;
public class ServerThread implements Runnable{
final DataInputStream inputStream;
......@@ -51,10 +56,12 @@ public class ServerThread implements Runnable{
// Switch for what to do based on what's given
switch (received) {
case "0" -> {
updateDatabase(0);
System.out.println("Nobody Detected");
outputStream.writeChar('d');
}
case "1" -> {
updateDatabase(1);
System.out.println("Somebody Detected");
outputStream.writeChar('a');
}
......@@ -68,4 +75,23 @@ public class ServerThread implements Runnable{
}
}
}
}
\ No newline at end of file
private void updateDatabase(int value) {
String sql = String.format("insert into presence (presentOrNot, secondsSinceEpoch)\n" +
"values (%d, %d);", value, Instant.now().getEpochSecond());
String url = "jdbc:sqlite:/Users/jonathanp/Code/databases/iot/iotserver";
try (Connection conn = DriverManager.getConnection(url);
Statement stmt = conn.createStatement()) {
try {
stmt.executeUpdate(sql);
} catch (SQLException e ) {
throw new Error("Problem", e);
}
} catch (SQLException e) {
throw new Error("Problem", e);
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment