/** * 更新任务 * * @param task * @throws SQLException */ public long update(Task task) throws SQLException { // ID,NAME,STATUS,CTIME,FTIME,MESSAGE String upSql = "update " + TABLE_NAME + " set NAME=?,STATUS=?,CTIME=?,FTIME=?,MESSAGE=? where ID=?"; Connection conn = null; PreparedStatement upStmt = null; try { conn = DBUtil.getConnection(); conn.setAutoCommit(false); upStmt = conn.prepareStatement(upSql); upStmt.setString(1, task.getName()); upStmt.setInt(2, task.getStatus()); upStmt.setLong(3, task.getcTime()); upStmt.setLong(4, task.getfTime()); upStmt.setString(5, task.getMessage()); upStmt.setLong(6, task.getId()); upStmt.execute(); this.orderDAO.update(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 (upStmt != null) { upStmt.close(); } if (conn != null) { conn.close(); } } }
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(); } } }