/** update a project */ public void update(Project project) { PreparedStatement stmt = null; try { String sql = "update Projects " + "set Title = ?, RecordSperImage = ?, FirstYCoord = ?, RecordHeight = ? " + "where ProjectID = ?"; stmt = db.getConnection().prepareStatement(sql); stmt.setString(1, project.getTitle()); stmt.setInt(2, project.getRecordsPerImage()); stmt.setInt(3, project.getFirstYCoord()); stmt.setInt(4, project.getRecordHeight()); stmt.setInt(5, project.getProjectID()); if (stmt.executeUpdate() == 1) { System.out.println("Update success"); } else { System.out.println("Update fail"); } } catch (SQLException e) { System.out.println("Can't execute update"); e.printStackTrace(); } finally { try { if (stmt != null) stmt.close(); } catch (SQLException e) { System.out.println("Can't execute connect"); e.printStackTrace(); } } }
/** add a project */ public void add(Project project) { PreparedStatement stmt = null; Statement keyStmt = null; ResultSet keyRS = null; try { String sql = "insert into Projects " + "(Title, RecordSperImage, " + "FirstYCoord, RecordHeight) " + "values (?, ?, ?, ?)"; stmt = db.getConnection().prepareStatement(sql); stmt.setString(1, project.getTitle()); stmt.setInt(2, project.getRecordsPerImage()); stmt.setInt(3, project.getFirstYCoord()); stmt.setInt(4, project.getRecordHeight()); if (stmt.executeUpdate() == 1) { keyStmt = db.getConnection().createStatement(); keyRS = keyStmt.executeQuery("select last_insert_rowid()"); keyRS.next(); int id = keyRS.getInt(1); project.setProjectID(id); } else { System.out.println("Add fail"); } } catch (SQLException e) { System.out.println("Can't execute add"); e.printStackTrace(); } finally { try { if (keyRS != null) keyRS.close(); if (stmt != null) stmt.close(); if (keyStmt != null) keyStmt.close(); } catch (SQLException e) { System.out.println("Can't execute connect"); e.printStackTrace(); } } }