我的程序总是停止插入数据库表而不会崩溃。这是我的代码
创建数据源和连接:
public void setUp() throws Exception {
manager = new IngredientManager();
BasicDataSource ds = new BasicDataSource();
ds.setUrl("jdbc:derby:memory:ingredient;create=true");
manager.setDataSource(ds);
manager.createTables();
}
方法创建表():
public void createTables() throws ServiceFailureException, SQLException {
Connection con = dataSource.getConnection();
con.setAutoCommit(false);
String createTableSQL = "CREATE TABLE INGREDIENTS("
+ "ID BIGINT NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY, "
+ "NAME VARCHAR(255), "
+ "AMOUNT DOUBLE, "
+ "UNIT VARCHAR(255), "
+ "RECIPEID INTEGER NOT NULL "
+ ")";
PreparedStatement query = con.prepareStatement(createTableSQL);
query.executeUpdate();
}
这一切正常,但是当程序涉及到这个时:
Connection connection = dataSource.getConnection();
connection.setAutoCommit(false);
query = connection.prepareStatement("INSERT INTO INGREDIENTS (NAME, AMOUNT, UNIT, RECIPEID) VALUES(?, ?, ?, ?)", Statement.RETURN_GENERATED_KEYS);
在connection.prepareStatement 程序之后没有做任何其他事情,只是永远构建。任何想法为什么这可能会被破坏?谢谢