0

这个连接类

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;
}

这种插入方法

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(); 
    }
}

测试显示一切正常,请求已通过,但在 DB 中没有任何新内容。getAll 和 getById(ResultSet) - 方法正常工作。内存中的数据库。在数据库“只读”模式的文件属性中关闭。

4

1 回答 1

0

我有hslqdb.jar,他创建db/library,要满足测试要求,in-process模式。我删除了 SQL 类中的connection.commit()and connection.setAutoCommit(true),一切正常。

于 2019-12-19T09:16:03.190 回答