Fix addAnswer
This commit is contained in:
parent
e93f3980f2
commit
5abb06ef58
1 changed files with 41 additions and 36 deletions
|
@ -1,51 +1,55 @@
|
|||
package servlet;
|
||||
|
||||
import java.sql.*;
|
||||
|
||||
/*
|
||||
* Class for managing the database.
|
||||
*/
|
||||
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");
|
||||
|
||||
// Display the contents of the database in the console.
|
||||
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()) {
|
||||
ResultSet rs = stmt.executeQuery("select * from Respondents");
|
||||
try (Statement stmt = conn.createStatement()) {
|
||||
ResultSet rs = stmt.executeQuery("select * from Respondents");
|
||||
while (rs.next()) {
|
||||
String name = rs.getString("name");
|
||||
String name = rs.getString("name");
|
||||
System.out.println(name);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue