java - 不要通过插入/删除/创建请求 JDBC
问题描述
这个连接类
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) - 方法正常工作。内存中的数据库。在数据库“只读”模式的文件属性中关闭。
解决方案
我有hslqdb.jar,他创建db/library,要满足测试要求,in-process模式。我删除了 SQL 类中的connection.commit()
and connection.setAutoCommit(true)
,一切正常。
推荐阅读
- swift - 保护让数字作为 NSString 和 NSNumber
- javascript - 使用 ReactJS 创建可调节计时器时遇到问题,将子项的状态更改为父项
- swift - 在 swift 包管理器中减少 MongoRealm(领域,领域数据库)的大小
- c# - 如何在 appsettings.json 中使用多个单独的记录器 Serilog 文件
- r - R中有没有办法在不使用布局中的heights语句的情况下自定义图形的高度比例?
- php - Laravel Paypal POST 请求被拒绝
- ionic4 - 如何在卡 Ionic 上有圆形按钮
- python - 什么可以解决下面提到的错误?
- javascript - 使用 jQuery 在当前页面中搜索
- vb.net - 如何防止在 vb.net 文本文件的文本框中出现换行符 (vbLf) 和回车符 (vbCr)?