0

This connection class

public class AppConnect {
private static final String url ="jdbc:hsqldb:file:src/main/resources/db/library";
private static final String driver ="org.hsqldb.jdbcDriver";
private static final String user = "SA";
private static final String password = "";
public Connection getConnection() {
    Connection connection = null;
    try {
        Class.forName(driver);
        connection = DriverManager.getConnection(url, user, password);
        connection.setAutoCommit(true);
        System.out.println("+)");
    } catch (ClassNotFoundException | SQLException e) {
        e.printStackTrace();
        System.out.println(":(");
    }
    return connection;
}

} This kinda insert method

public class AuthorSQL extends AppConnect implements AuthorsDAO {
Connection connection = getConnection();
@Override
public void add(Author author) throws SQLException {
    PreparedStatement pr = null;
    String sql = "INSERT INTO AUTHORS(ID, NAME, SURNAME, FATHERNAME) VALUES ( ?, ?, ?, ?)";
    try {
        System.out.println(connection.isClosed());
        connection.setAutoCommit(true);
        pr = connection.prepareStatement(sql);

        pr.setLong(1, author.getId());
        pr.setString(2, author.getName());
        pr.setString(3, author.getSurname());
        pr.setString(4, author.getFathername());
        pr.executeUpdate();
        connection.commit();
        System.out.println("success");
    } catch (SQLException e) {e.printStackTrace();
    } finally {
        pr.close();
        connection.close(); 
    }
}

Test shows all ok, request is passed, but in DB nothing new. getAll and getById(ResultSet) - methods works normal. DB in-memory. In the file properties of DB "only read" mode off.

Krit
  • 1
  • 1
  • `src/main/resources/db/library` is a suspicious location for a database. Resources are normally in the application jar file and not writeable, and the way you reference it will only work from within your IDE. – Mark Rotteveel Dec 18 '19 at 13:15
  • On a separate note, calling `connection.commit()` while auto-commit is true should result in an exception. When auto-commit is used, the driver (or database) should commit automatically when the statement has been executed. In any case, please post a [mre], as your current code is missing some parts. Please make sure to reduce your code to the minimum necessary to reproduce the problem. – Mark Rotteveel Dec 18 '19 at 13:43

1 Answers1

0

I have hslqdb.jar, and he creates db/library, it is necessary to meet the requirements of the test, in-process mode. I remove connection.commit() and connection.setAutoCommit(true) in SQL class, and all worked.

Krit
  • 1
  • 1