/** * insert * * @param order * @return */ @Override public int insert(Order order) { int orderId = order.getOrderId(); int userId = order.getUserId(); int ready = order.getReady(); LinkedList<Integer> dishIdList = order.getDishIDList(); Connection connection = MyConnection.getInstance().getConnection(); try { PreparedStatement preparedStatement = connection.prepareStatement( "INSERT INTO order_table (orderId, userId, ready, summ) " + "VALUES (?, ?, 0, 0)"); preparedStatement.setInt(1, orderId); preparedStatement.setInt(2, userId); preparedStatement.execute(); PreparedStatement preparedStatement2 = connection.prepareCall("select last_insert_id();"); ResultSet resultSet2 = preparedStatement2.executeQuery(); if (resultSet2.next()) { orderId = resultSet2.getInt(MYSQL_LAST_INSERT_ID.toString()); } connection.setAutoCommit(false); for (int i = 0; i < dishIdList.size(); i++) { PreparedStatement preparedStatement1 = connection.prepareStatement( "INSERT INTO order_content (orderId, dishId) " + "VALUES (?, ?)"); preparedStatement1.setInt(1, orderId); preparedStatement1.setInt(2, dishIdList.get(i)); preparedStatement1.execute(); } connection.commit(); connection.setAutoCommit(true); } catch (SQLException | NullPointerException ex) { Logger.getLogger(MySQLOrderDAO.class.getName()).error(ex); } finally { try { if (connection != null) { connection.close(); } } catch (SQLException ex) { Logger.getLogger(MySQLOrderDAO.class.getName()).error(ex); } } return orderId; }