0
我的程序總是停止插入數據庫表而不會崩潰。這裏是我的代碼用prepareStatement在java中插入數據庫
創建數據源和連接:
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();
}
方法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程序沒有做任何事情之後,只是永遠建立。任何想法,爲什麼這可能會損壞?謝謝
你是什麼意思與_building forever_?你如何驗證下面的代碼永遠不會被執行? – jlordo 2013-03-26 13:57:28
我有方法query = connection.prepareStatement(..)上的斷點和方法後的斷點,程序將永遠不會到達第二個斷點 – 2013-03-26 13:59:50
刪除第一個斷點。現在是否會接觸到另一個? – jlordo 2013-03-26 14:00:48