diff --git a/src/org/kar/archidata/SqlWrapper.java b/src/org/kar/archidata/SqlWrapper.java index b0e14f5..4f11e1d 100644 --- a/src/org/kar/archidata/SqlWrapper.java +++ b/src/org/kar/archidata/SqlWrapper.java @@ -395,7 +395,7 @@ public class SqlWrapper { return insert(data); } - public static void update(Class clazz, long id, String jsonData) throws Exception { + public static int update(Class clazz, long id, String jsonData) throws Exception { ObjectMapper mapper = new ObjectMapper(); // parse the object to be sure the data are valid: T data = mapper.readValue(jsonData, clazz); @@ -404,10 +404,19 @@ public class SqlWrapper { List keys = new ArrayList<>(); Iterator iterator = root.fieldNames(); iterator.forEachRemaining(e -> keys.add(e)); - update(data, id, keys); + return update(data, id, keys); } - public static void update(T data, long id, List filterValue) throws Exception { + /** + * + * @param + * @param data + * @param id + * @param filterValue + * @return the affected rows. + * @throws Exception + */ + public static int update(T data, long id, List filterValue) throws Exception { Class clazz = data.getClass(); //public static NodeSmall createNode(String typeInNode, String name, String description, Long parentId) { @@ -515,14 +524,14 @@ public class SqlWrapper { ps.setLong(iii++, id); // execute the request int affectedRows = ps.executeUpdate(); - // todo manage the list of element is valid ... - //ps.execute(); + return affectedRows; } catch (SQLException ex) { ex.printStackTrace(); } finally { entry.close(); entry = null; } + return 0; } static void addElement(PreparedStatement ps, Object value, int iii) throws Exception { diff --git a/src/org/kar/archidata/db/DBEntry.java b/src/org/kar/archidata/db/DBEntry.java index 7818d02..1c2ff7c 100644 --- a/src/org/kar/archidata/db/DBEntry.java +++ b/src/org/kar/archidata/db/DBEntry.java @@ -1,7 +1,5 @@ package org.kar.archidata.db; -import org.kar.archidata.model.User; - import java.io.Closeable; import java.io.IOException; import java.sql.*; @@ -10,16 +8,16 @@ public class DBEntry implements Closeable { public DBConfig config; public Connection connection; - public DBEntry(DBConfig config) { + public DBEntry(DBConfig config) throws IOException { this.config = config; connect(); } - public void connect() { + public void connect() throws IOException { try { connection = DriverManager.getConnection(config.getUrl(), config.getLogin(), config.getPassword()); } catch (SQLException ex) { - ex.printStackTrace(); + throw new IOException("Connection db fail: " + ex.getMessage()); } } @@ -42,7 +40,7 @@ public class DBEntry implements Closeable { //connection.commit(); connection.close(); } catch (SQLException ex) { - ex.printStackTrace(); + throw new IOException("Dis-connection db fail: " + ex.getMessage()); } } diff --git a/src/org/kar/archidata/model/GenericTable.java b/src/org/kar/archidata/model/GenericTable.java index 8134c8e..41ab29c 100644 --- a/src/org/kar/archidata/model/GenericTable.java +++ b/src/org/kar/archidata/model/GenericTable.java @@ -26,10 +26,12 @@ public class GenericTable { @SQLCreateTime @SQLNotNull @SQLComment("Create time of the object") + @SQLDefault("now()") public Timestamp create_date = null; @SQLNotRead @SQLUpdateTime @SQLNotNull @SQLComment("When update the object") + @SQLDefault("now()") public Timestamp modify_date = null; }