不传递插入/删除/创建请求JDBC

问题描述 投票:0回答:1

此连接类别

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)-方法正常工作。数据库内存。在数据库的文件属性中,“仅读取”模式关闭。

java jdbc insert-query
1个回答
0
投票

我有hslqdb.jar,他创建了db / library,有必要满足测试过程中模式的要求。我在SQL类中删除了connection.commit()connection.setAutoCommit(true),并且全部正常。

© www.soinside.com 2019 - 2024. All rights reserved.