Fix addAnswer

This commit is contained in:
Imbus 2024-02-13 16:53:44 +01:00
parent e93f3980f2
commit 5abb06ef58

View file

@ -1,51 +1,55 @@
package servlet; package servlet;
import java.sql.*; import java.sql.*;
/* /*
* Class for managing the database. * Class for managing the database.
*/ */
public class Database implements AutoCloseable { public class Database implements AutoCloseable {
// If you have the mysql server on your own computer use "localhost" as server address. // If you have the mysql server on your own computer use "localhost" as server
// address.
private static String databaseServerAddress = "vm26.cs.lth.se"; private static String databaseServerAddress = "vm26.cs.lth.se";
private static String databaseUser = "<your databse user on vm26>"; // database login user private static String databaseUser = "<your databse user on vm26>"; // database login user
private static String databasePassword = "<your password>"; // database login password private static String databasePassword = "<your password>"; // database login password
private static String database = "<your database, normally the same as database user>"; // the database to use, i.e. default schema private static String database = "<your database, normally the same as database user>"; // the database to use, i.e.
// default schema
private Connection conn = null; private Connection conn = null;
public Database() { public Database() {
try{ try {
conn = DriverManager.getConnection("jdbc:sqlite:/home/thugborean/Documents/projects/java/etsf20-test/lab.db3"); conn = DriverManager
.getConnection("jdbc:sqlite:/home/thugborean/Documents/projects/java/etsf20-test/lab.db3");
// Display the contents of the database in the console.
// Display the contents of the database in the console.
// This should be removed in the final version // This should be removed in the final version
try(Statement stmt = conn.createStatement()) { try (Statement stmt = conn.createStatement()) {
ResultSet rs = stmt.executeQuery("select * from Respondents"); ResultSet rs = stmt.executeQuery("select * from Respondents");
while (rs.next()) { while (rs.next()) {
String name = rs.getString("name"); String name = rs.getString("name");
System.out.println(name); System.out.println(name);
} }
} }
} catch (SQLException e) { } catch (SQLException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }
public boolean addName(String name) { public boolean addName(String name) {
String sql = "insert into Respondents (name) values(?)"; String sql = "insert into Respondents (name) values(?)";
try(PreparedStatement ps = conn.prepareStatement(sql)) { try (PreparedStatement ps = conn.prepareStatement(sql)) {
ps.setString(1, name); ps.setString(1, name);
ps.executeUpdate(); ps.executeUpdate();
return true; return true;
} catch (SQLException e) { } catch (SQLException e) {
if(e.getErrorCode()==1062 && e.getSQLState().equals("23000")){ if (e.getErrorCode() == 1062 && e.getSQLState().equals("23000")) {
// duplicate key error // duplicate key error
System.out.println(name + " already exists in the database"); System.out.println(name + " already exists in the database");
} else { } else {
printSqlError(e); printSqlError(e);
} }
return false; return false;
} }
// Using the try-with-resources syntax, this will call ps.close() automatically // Using the try-with-resources syntax, this will call ps.close() automatically
@ -54,29 +58,30 @@ public class Database implements AutoCloseable {
public boolean addAnswer(String name, String answerOne, String answerTwo, String answerThree, String 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); ps.setString(1, answerOne);
ps.setString(2, answerTwo); ps.setString(2, answerTwo);
ps.setString(3, answerThree); ps.setString(3, answerThree);
ps.setString(4, answerFour); ps.setString(4, answerFour);
ps.setString(5, name); ps.setString(5, name);
ps.executeUpdate();
return true; return true;
} catch (SQLException e) { } catch (SQLException e) {
if(e.getErrorCode()==1062 && e.getSQLState().equals("23000")) { if (e.getErrorCode() == 1062 && e.getSQLState().equals("23000")) {
// duplicate key error // duplicate key error
System.out.println(name + " already exists in the database"); System.out.println(name + " already exists in the database");
} else { } else {
printSqlError(e); printSqlError(e);
}
return false;
} }
return false;
}
} }
private void printSqlError(SQLException e){ private void printSqlError(SQLException e) {
System.out.println("SQLException: " + e.getMessage()); System.out.println("SQLException: " + e.getMessage());
System.out.println("SQLState: " + e.getSQLState()); System.out.println("SQLState: " + e.getSQLState());
System.out.println("VendorError: " + e.getErrorCode()); System.out.println("VendorError: " + e.getErrorCode());
throw new RuntimeException(e); throw new RuntimeException(e);
} }