addAnswer

This commit is contained in:
borean 2024-02-12 17:00:47 +01:00
parent 9a92ea66b3
commit 213a8c6571
3 changed files with 24 additions and 5 deletions

View file

@ -51,16 +51,25 @@ public class Database implements AutoCloseable {
// Using the try-with-resources syntax, this will call ps.close() automatically // 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(?,?,?,?,?)"; String sql = "insert into Answer (answerOne, answerTwo, answerThree, answerFour, name) values(?,?,?,?,?)";
try(PreparedStatement ps = conn.prepareStatement(sql)) { try(PreparedStatement ps = conn.prepareStatement(sql)) {
ps.setString(1, answerOne);
} catch (Exception e) { ps.setString(2, answerTwo);
// TODO: handle exception ps.setString(3, answerThree);
return false; 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; return false;
}
} }

View file

@ -51,6 +51,14 @@ public class Dump extends HttpServlet {
return value > 0 && value <11; 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) * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/ */

View file

@ -113,6 +113,8 @@ public class Survey extends HttpServlet {
s12 = Integer.parseInt(s12String); s12 = Integer.parseInt(s12String);
s13 = Integer.parseInt(s13String); s13 = Integer.parseInt(s13String);
s14 = Integer.parseInt(s14String); s14 = Integer.parseInt(s14String);
String str = s11String+s12String+s13String;
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
valuesOk = false; valuesOk = false;
} }