Commit 72962be3 authored by vehjelmtvedt's avatar vehjelmtvedt
Browse files

SessionStorage now use prepared statements

parent 97d51e18
package inf226.inchat;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.*;
import java.time.Instant;
import java.util.UUID;
......@@ -32,11 +29,13 @@ public final class SessionStorage
throws SQLException {
final Stored<Session> stored = new Stored<Session>(session);
String sql = "INSERT INTO Session VALUES('" + stored.identity + "','"
+ stored.version + "','"
+ session.account.identity + "','"
+ session.expiry.toString() + "')";
connection.createStatement().executeUpdate(sql);
PreparedStatement statement = connection.prepareStatement("INSERT INTO Session VALUES(?,?,?,?)");
statement.setObject(1, stored.identity);
statement.setObject(2, stored.version);
statement.setObject(3, session.account.identity);
statement.setString(4, session.expiry.toString());
statement.executeUpdate();
return stored;
}
......@@ -49,13 +48,13 @@ public final class SessionStorage
final Stored<Session> current = get(session.identity);
final Stored<Session> updated = current.newVersion(new_session);
if(current.version.equals(session.version)) {
String sql = "UPDATE Session SET" +
" (version,account,expiry) =('"
+ updated.version + "','"
+ new_session.account.identity + "','"
+ new_session.expiry.toString()
+ "') WHERE id='"+ updated.identity + "'";
connection.createStatement().executeUpdate(sql);
PreparedStatement statement = connection.prepareStatement("UPDATE Session SET (version,account,expiry) =(?,?,?) WHERE id=?");
statement.setObject(1, updated.version);
statement.setObject(2, new_session.account.identity);
statement.setString(3, new_session.expiry.toString());
statement.setObject(4, updated.identity);
statement.executeUpdate();
} else {
throw new UpdatedException(current);
}
......@@ -69,8 +68,9 @@ public final class SessionStorage
SQLException {
final Stored<Session> current = get(session.identity);
if(current.version.equals(session.version)) {
String sql = "DELETE FROM Session WHERE id ='" + session.identity + "'";
connection.createStatement().executeUpdate(sql);
PreparedStatement statement = connection.prepareStatement("DELETE FROM Session WHERE id =?");
statement.setObject(1, session.identity);
statement.executeUpdate();
} else {
throw new UpdatedException(current);
}
......@@ -79,9 +79,10 @@ public final class SessionStorage
public Stored<Session> get(UUID id)
throws DeletedException,
SQLException {
final String sql = "SELECT version,account,expiry FROM Session WHERE id = '" + id.toString() + "'";
final Statement statement = connection.createStatement();
final ResultSet rs = statement.executeQuery(sql);
PreparedStatement statement = connection.prepareStatement("SELECT version,account,expiry FROM Session WHERE id = ?");
statement.setString(1, id.toString());
final ResultSet rs = statement.executeQuery();
if(rs.next()) {
final UUID version = UUID.fromString(rs.getString("version"));
......
<?xml version="1.0" encoding="UTF-8"?>
<testsuite xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://maven.apache.org/surefire/maven-surefire-plugin/xsd/surefire-test-report.xsd" name="inf226.inchat.InchatTest" time="0.395" tests="1" errors="0" skipped="0" failures="0">
<testsuite xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://maven.apache.org/surefire/maven-surefire-plugin/xsd/surefire-test-report.xsd" name="inf226.inchat.InchatTest" time="0.397" tests="1" errors="0" skipped="0" failures="0">
<properties>
<property name="java.specification.version" value="15"/>
<property name="sun.jnu.encoding" value="UTF-8"/>
......@@ -12,7 +12,7 @@
<property name="sun.java.launcher" value="SUN_STANDARD"/>
<property name="user.country" value="GB"/>
<property name="sun.boot.library.path" value="/Library/Java/JavaVirtualMachines/jdk-15.0.1.jdk/Contents/Home/lib"/>
<property name="sun.java.command" value="/Users/vetle/Desktop/Skole/5. Semester/INF226/Oblig 3/inf226-2021-inchat/target/surefire/surefirebooter16480299950810032626.jar /Users/vetle/Desktop/Skole/5. Semester/INF226/Oblig 3/inf226-2021-inchat/target/surefire 2021-11-08T17-30-57_389-jvmRun1 surefire278319907713983728tmp surefire_02992833018309938155tmp"/>
<property name="sun.java.command" value="/Users/vetle/Desktop/Skole/5. Semester/INF226/Oblig 3/inf226-2021-inchat/target/surefire/surefirebooter16727841168076760306.jar /Users/vetle/Desktop/Skole/5. Semester/INF226/Oblig 3/inf226-2021-inchat/target/surefire 2021-11-08T17-36-40_363-jvmRun1 surefire12686560047660004962tmp surefire_014934364296445214328tmp"/>
<property name="http.nonProxyHosts" value="local|*.local|169.254/16|*.169.254/16"/>
<property name="jdk.debug" value="release"/>
<property name="surefire.test.class.path" value="/Users/vetle/Desktop/Skole/5. Semester/INF226/Oblig 3/inf226-2021-inchat/target/test-classes:/Users/vetle/Desktop/Skole/5. Semester/INF226/Oblig 3/inf226-2021-inchat/target/classes:/Users/vetle/.m2/repository/org/eclipse/jetty/jetty-server/9.4.9.v20180320/jetty-server-9.4.9.v20180320.jar:/Users/vetle/.m2/repository/javax/servlet/javax.servlet-api/3.1.0/javax.servlet-api-3.1.0.jar:/Users/vetle/.m2/repository/org/eclipse/jetty/jetty-http/9.4.9.v20180320/jetty-http-9.4.9.v20180320.jar:/Users/vetle/.m2/repository/org/eclipse/jetty/jetty-util/9.4.9.v20180320/jetty-util-9.4.9.v20180320.jar:/Users/vetle/.m2/repository/org/eclipse/jetty/jetty-io/9.4.9.v20180320/jetty-io-9.4.9.v20180320.jar:/Users/vetle/.m2/repository/org/junit/jupiter/junit-jupiter-api/5.4.2/junit-jupiter-api-5.4.2.jar:/Users/vetle/.m2/repository/org/apiguardian/apiguardian-api/1.0.0/apiguardian-api-1.0.0.jar:/Users/vetle/.m2/repository/org/opentest4j/opentest4j/1.1.1/opentest4j-1.1.1.jar:/Users/vetle/.m2/repository/org/junit/platform/junit-platform-commons/1.4.2/junit-platform-commons-1.4.2.jar:/Users/vetle/.m2/repository/org/junit/jupiter/junit-jupiter-engine/5.4.2/junit-jupiter-engine-5.4.2.jar:/Users/vetle/.m2/repository/org/junit/platform/junit-platform-engine/1.4.2/junit-platform-engine-1.4.2.jar:/Users/vetle/.m2/repository/org/xerial/sqlite-jdbc/3.28.0/sqlite-jdbc-3.28.0.jar:/Users/vetle/.m2/repository/com/lambdaworks/scrypt/1.4.0/scrypt-1.4.0.jar:"/>
......@@ -28,7 +28,7 @@
<property name="line.separator" value="&#10;"/>
<property name="java.specification.name" value="Java Platform API Specification"/>
<property name="java.vm.specification.vendor" value="Oracle Corporation"/>
<property name="surefire.real.class.path" value="/Users/vetle/Desktop/Skole/5. Semester/INF226/Oblig 3/inf226-2021-inchat/target/surefire/surefirebooter16480299950810032626.jar"/>
<property name="surefire.real.class.path" value="/Users/vetle/Desktop/Skole/5. Semester/INF226/Oblig 3/inf226-2021-inchat/target/surefire/surefirebooter16727841168076760306.jar"/>
<property name="sun.management.compiler" value="HotSpot 64-Bit Tiered Compilers"/>
<property name="ftp.nonProxyHosts" value="local|*.local|169.254/16|*.169.254/16"/>
<property name="java.runtime.version" value="15.0.1+9-18"/>
......@@ -54,5 +54,5 @@
<property name="socksNonProxyHosts" value="local|*.local|169.254/16|*.169.254/16"/>
<property name="java.class.version" value="59.0"/>
</properties>
<testcase name="chatSetup" classname="inf226.inchat.InchatTest" time="0.387"/>
<testcase name="chatSetup" classname="inf226.inchat.InchatTest" time="0.391"/>
</testsuite>
\ No newline at end of file
-------------------------------------------------------------------------------
Test set: inf226.inchat.InchatTest
-------------------------------------------------------------------------------
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.395 s - in inf226.inchat.InchatTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.397 s - in inf226.inchat.InchatTest
Supports Markdown
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