public static void main(String[] args) throws SQLException { TaskDAO taskDAO = new TaskDAO(); long id = 2; Task task = taskDAO.getById(id); System.out.println("getName = " + task.getName()); System.out.println("getcTime = " + task.getcTime()); System.out.println("getfTime = " + task.getfTime()); System.out.println("getStatus = " + task.getStatus()); System.out.println("getMessage = " + task.getMessage()); System.out.println("getType = " + task.getType()); System.out.println("getOrders = " + task.getOrders().size()); for (Order order : task.getOrders()) { System.out.println("order ......."); System.out.println("\tgetId = " + order.getId()); System.out.println("\tgetMessage = " + order.getMessage()); System.out.println("\tgetTid = " + order.getTid()); System.out.println("\tgetClass = " + order.getClass()); } }
/** * @param task * @throws SQLException */ public long add(Task task) throws SQLException { String insertSQL = "insert into " + TABLE_NAME + "(ID,NAME,STATUS,CTIME,FTIME,MESSAGE,TYPE) values(?,?,?,?,?,?,?)"; ; Connection conn = null; PreparedStatement inStmt = null; boolean autoCommit = false; try { conn = DBUtil.getConnection(); autoCommit = conn.getAutoCommit(); conn.setAutoCommit(false); inStmt = conn.prepareStatement(insertSQL); inStmt.setLong(1, task.getId()); inStmt.setString(2, task.getName()); inStmt.setInt(3, task.getStatus()); inStmt.setLong(4, task.getcTime()); inStmt.setLong(5, task.getfTime()); inStmt.setString(6, task.getMessage()); inStmt.setString(7, task.getType()); inStmt.execute(); orderDAO.add(conn, task.getOrders()); conn.commit(); return task.getId(); } catch (Exception e) { LOG.error("Will rollback"); conn.rollback(); LOG.error(e.getMessage(), e); throw new RuntimeException(e); } finally { if (conn != null) { conn.setAutoCommit(autoCommit); conn.close(); } if (inStmt != null) { inStmt.close(); } } }