gpt4 book ai didi

java - 不要通过 JDBC 传递 Insert/drop/create 请求

转载 作者:行者123 更新时间:2023-12-01 19:18:21 25 4
gpt4 key购买 nike

该连接类

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

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

最佳答案

我有hslqdb.jar,他创建db/library,需要满足测试的要求,进程内模式。我删除了 SQL 类中的 connection.commit()connection.setAutoCommit(true),并且一切正常。

关于java - 不要通过 JDBC 传递 Insert/drop/create 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59391514/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com