diff --git a/src/main/java/servlet/Database.java b/src/main/java/servlet/Database.java index 89edd0b..c63186f 100644 --- a/src/main/java/servlet/Database.java +++ b/src/main/java/servlet/Database.java @@ -51,16 +51,25 @@ public class Database implements AutoCloseable { // Using the try-with-resources syntax, this will call ps.close() automatically } - public boolean addAnswer(String name, int answerOne, int answerTwo, int answerThree, int answerFour) { + public boolean addAnswer(String name, String answerOne, String answerTwo, String answerThree, String answerFour) { String sql = "insert into Answer (answerOne, answerTwo, answerThree, answerFour, name) values(?,?,?,?,?)"; try(PreparedStatement ps = conn.prepareStatement(sql)) { - - } catch (Exception e) { - // TODO: handle exception - return false; + ps.setString(1, answerOne); + ps.setString(2, answerTwo); + ps.setString(3, answerThree); + ps.setString(4, answerFour); + ps.setString(5, name); + return true; + } catch (SQLException e) { + if(e.getErrorCode()==1062 && e.getSQLState().equals("23000")) { + // duplicate key error + System.out.println(name + " already exists in the database"); + } else { + printSqlError(e); } return false; + } } diff --git a/src/main/java/servlet/Dump.java b/src/main/java/servlet/Dump.java index a8f88ba..ff3ef1e 100644 --- a/src/main/java/servlet/Dump.java +++ b/src/main/java/servlet/Dump.java @@ -51,6 +51,14 @@ public class Dump extends HttpServlet { return value > 0 && value <11; } + /// Write fn that takes params and inputs to db, returns true if succ else false if fail + // test(str a, str b, str c) +protected boolean updateAnswers(String answerOne, String answerTwo) ( + /// logic + + return true; +) + /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ diff --git a/src/main/java/servlet/Survey.java b/src/main/java/servlet/Survey.java index bc71a6a..7331ed2 100644 --- a/src/main/java/servlet/Survey.java +++ b/src/main/java/servlet/Survey.java @@ -113,6 +113,8 @@ public class Survey extends HttpServlet { s12 = Integer.parseInt(s12String); s13 = Integer.parseInt(s13String); s14 = Integer.parseInt(s14String); + + String str = s11String+s12String+s13String; } catch (NumberFormatException e) { valuesOk = false; }