Fix addAnswer

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

View file

@ -1,4 +1,5 @@
package servlet;
import java.sql.*;
/*
@ -6,20 +7,23 @@ import java.sql.*;
*/
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 databaseUser = "<your databse user on vm26>"; // database login user
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 databaseUser = "<your databse user on vm26>"; // database login user
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 Connection conn = null;
public Database() {
try{
conn = DriverManager.getConnection("jdbc:sqlite:/home/thugborean/Documents/projects/java/etsf20-test/lab.db3");
try {
conn = DriverManager
.getConnection("jdbc:sqlite:/home/thugborean/Documents/projects/java/etsf20-test/lab.db3");
// Display the contents of the database in the console.
// 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");
while (rs.next()) {
String name = rs.getString("name");
@ -35,17 +39,17 @@ public class Database implements AutoCloseable {
public boolean addName(String name) {
String sql = "insert into Respondents (name) values(?)";
try(PreparedStatement ps = conn.prepareStatement(sql)) {
try (PreparedStatement ps = conn.prepareStatement(sql)) {
ps.setString(1, name);
ps.executeUpdate();
return true;
ps.executeUpdate();
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 {
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;
}
// 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) {
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(2, answerTwo);
ps.setString(3, answerThree);
ps.setString(4, answerFour);
ps.setString(5, name);
ps.executeUpdate();
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);
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;
}
}
private void printSqlError(SQLException e){
System.out.println("SQLException: " + e.getMessage());
System.out.println("SQLState: " + e.getSQLState());
System.out.println("VendorError: " + e.getErrorCode());
private void printSqlError(SQLException e) {
System.out.println("SQLException: " + e.getMessage());
System.out.println("SQLState: " + e.getSQLState());
System.out.println("VendorError: " + e.getErrorCode());
throw new RuntimeException(e);
}